Sort explanation
Sort explanation
Core Concepts:
- Starting with the first index, selection sort finds the index with the smallest
value (or min for short)
- It then swaps the value with the first index of the unsorted array
- After swapping, the process continues with the searching for the next index
with smallest value
- The algorithm loops until there’s only one unsorted element left
Step-by-step explanation:
Original array: | 67 25 18 33 55
- Step 3: | 67 25 18 33 55 (continues
to find min)
- Step 5: 18 | 25 67 33 55 (after
reaching the end, swap min with the
first index of unsorted, min is now
sorted)
- Space: Due to the fact that selection directly changes the array, it has the
space complexity of: O(1)
Bubble sort
Core Concepts:
- Starting with the first index, bubble sort checks whether the value of the
next index is higher or lower than the current index
If the next index value is lower, it then swaps the value of both indexes
and moves onto the next index
If the next index value is higher, it moves onto the next index
- This process repeats until it reaches the final index which, in theory, will
have the largest value and will be the first sorted index
- It then repeats itself, starting from the first index again, going until it
reaches the final unsorted index (which excludes the sorted indexes from
previous loops)
Step-by-step
explanation:
Original array: 7 2 5 8 4 |
Complexity analysis:
- Time: Similar to selection sort, bubble sort also has the time complexity of
O(n2). The difference is that instead of finding min through n, (n – 1), (n – 2)
elements, bubble sort goes through (n – 1), (n – 2) elements and swap each
other if the current index has greater value
- Space: Bubble sort have the space complexity of O(1) since it also directly
changes the array
Odd-even sort or simply brick sort, is one of the variants of bubble sort.
It works by comparing odd indexes and even indexes rather than each
index one at a time
Bubble sort also has its own bidirectional variant called cocktail shaker
sort. It processes similar to bubble sort but it changes back and forth
between checking if the current index is greater (from left to right) or if
the current index is smaller (from right to left)
Quick sort
Core Concepts:
- Firstly, a pivot is chosen (can be first, last or middle index)
- Divide the array into two smaller ones, smaller (<) & higher or equals to (≥)
the pivot’s value
- Repeat the pivot choosing in both arrays and continue to divide until only
one element remains
- Merge all elements together into one sorted array
Step-by-step explanation:
Original array: 9 10 3 18 27
- Step 3: 3 | 10 9 18 27 (Continue
choosing new pivot)
Complexity analysis:
- Time: The partitioning step, which nearly go through all the elements takes
O(n) time, while each subarrays of the original go through n/2 elements
using recursive. Therefore, quick sort has the time complexity of: 2T(n/2) +
O(n) which is O(n log n)
Best case: O(n log n) (for random, nearly-sorted data and middle pivot)
Worst case: O(n2) (for sorted, reversed data and first/last pivot)
Average case: O(n log n) (for random, nearly-sorted data and semi-middle
pivot)
- Space: With best/average case, quick sort splits the array into 2 subarrays
half/nearly half the original array, this occurs recursively giving quick sort the
space complexity of O(log n). On the other hand, if the data is
sorted/reversed and the pivot is either the first or last index, each subarray
has (n – 1) elements resulting a space complexity of O(n)
Best case: O(log n) (for random, nearly-sorted data and middle pivot)
Worst case: O(n) (for sorted, reversed data and last/first pivot)
- Variants:
Multi-pivot quick sort have the same premise as normal quick sort but
has the ability to pick multiple pivot instead
Quick radix sort is as expect a combination of both quick sort and radix
sort where instead of comparing each element’s value, it compares the
element’s digit instead
Flash sort
Core Concepts:
- First, flash sort search for the min and max value to estimate the range of
the array
- Then it divides the array and put them into different buckets/classes (using
a classification function) before counting the number of elements of each
buckets
- Continuing, flash sort adds the value of each bucket’s elements into an
accumulative sum
- Finally, the elements in each buckets are rearranged and sorted using
insertion sort
Step-by-step explanation:
Original array: 35 8 17 26 7
- Step 1: 35 | 8 17 26 7 [separate the
array into 2 buckets (by using the
formula m = n*0.43) with bucket
index]
- Step 4: 7 8 17 26 | 35 [elements in
bucket 0 are in their correct position
(index 0 to 4) so do nothing]
Complexity analysis:
- Time:
Best case: O(n) (buckets has nearly the same amount of elements)
- Space: O(m) with m being the amount of buckets (which are vectors)
Variants and optimizations: