SORTING ALGORITHMS

Sorting algorithms are used to sort a data structure according to a specific order relationship, such as numerical order or lexicographical order.
This operation is one of the most important and widespread in computer science. For a long time, new methods have been developed to make this procedure faster and faster.
There are currently hundreds of different sorting algorithms, each with its own specific characteristics. They are classified according to two metrics: space complexity and time complexity.
Those two kinds of complexity are represented with asymptotic notations, mainly with the symbols O, Θ, Ω, representing respectively the upper bound, the tight bound, and the lower bound of the algorithm's complexity, specifying in brackets an expression in terms of n, the number of the elements of the data structure.

Space and time complexity can also be further subdivided into 3 different cases: best case, average case and worst case.

Sorting algorithms can be difficult to understand and it's easy to get confused. We believe visualizing sorting algorithms can be a great way to better understand their functioning while having fun!

  • Bubble sort works on the repeatedly swapping of adjacent elements until they are not in the intended order. It is called bubble sort because the movement of array elements is just like the movement of air bubbles in the water. Bubbles in water rise up to the surface; similarly, the array elements in bubble sort move to the end in each iteration.
  • Although it is simple to use, it is primarily used as an educational tool because the performance of bubble sort is poor in the real world. It is not suitable for large data sets. The average and worst-case complexity of Bubble sort is O(n2), where n is a number of items.
  • Bubble short is majorly used where
    1. complexity does not matter
    2. simple and shortcode is preferred

BUBBLE SORT TIME COMPLEXITIES
Case Complexity
BEST O(n)
AVERAGE O(n^2)
WORST O(n^2)

EXECUTABLE CODE FOR BUBBLE SORT

BubbleSortSnippet

SELECTION SORT

  • Selection Sort is a simple comparison-based sorting algorithm that works by repeatedly finding the minimum element from the unsorted part of the list and putting it at the beginning of the list.
  • The algorithm works by dividing the input list into two parts: the sorted part and the unsorted part. Initially, the sorted part is empty, and the unsorted part is the entire list. The algorithm then iterates through the unsorted part of the list, finding the smallest element and moving it to the beginning of the list. This process is repeated until the entire list is sorted.
  • Here are the steps to perform selection sort on a list of n elements:
    1. Set the minimum index to the first element of the list.
    2. Iterate through the unsorted part of the list, comparing each element to the minimum element found so far. If a smaller element is found, update the minimum index to that element.
    3. Once the end of the unsorted part of the list is reached, swap the minimum element with the first element of the unsorted part of the list.
    4. Repeat steps 2-3 for the remaining elements in the unsorted part of the list, until the entire list is sorted
  • Selection sort has a time complexity of O(n^2) and is generally not recommended for large data sets. However, it is useful for educational purposes, as it is easy to understand and implement. It can also be useful for sorting small data sets where simplicity is more important than speed.


SELECTION SORT TIME COMPLEXITIES
Case Complexity
BEST O(n^2)
AVERAGE O(n^2)
WORST O(n^2)

EXECUTABLE SELECTION SORT CODE

SelectionSortCodeSnippet

INSERTION SORTING TECHNIQUE

  • Insertion Sort is a simple comparison-based sorting algorithm that builds the final sorted array one item at a time. It works by dividing the input list into two parts: the sorted part and the unsorted part. Initially, the sorted part is the first element of the list, and the unsorted part is the remaining n-1 elements.
  • The algorithm then iterates through the unsorted part of the list, taking one element at a time and inserting it into the correct position in the sorted part of the list. It does this by comparing the current element to each element in the sorted part of the list, starting from the right and moving left. If the current element is smaller than the element being compared, the compared element is shifted to the right, and the current element is inserted into its correct position.
  • Here are the steps to perform insertion sort on a list of n elements:
    1. Iterate through the unsorted part of the list, starting with the second element.
    2. For each element, compare it to the elements in the sorted part of the list, starting from the right and moving left.
    3. If the current element is smaller than the element being compared, shift the compared element to the right, and continue comparing with the next element in the sorted part of the list
    4. If the current element is larger than the element being compared, insert the current element into the position immediately to the right of the compared element.
    5. Repeat steps 2-4 for all remaining elements in the unsorted part of the list
  • Insertion sort has a time complexity of O(n^2), making it inefficient for large data sets. However, it is more efficient than other quadratic sorting algorithms, such as bubble sort and selection sort, when dealing with small data sets. It is also an adaptive algorithm, meaning it performs well on partially sorted lists.


INSERTION SORT TIME COMPLEXITIES
Case Complexity
BEST O(n)
AVERAGE O(n^2)
WORST O(n^2)

EXECUTABLE INSERTION SORT CODE

InsertionSortCodeSnippet