Comparison sort
A comparison sort is a type of sorting algorithm that only reads the list elements through a single abstract comparison operation (often a “less than or equal to” operator or a three-way comparison) that determines which of two elements should occur first in the final sorted list. — Wikipedia
Get Nicole Peng’s stories in your inbox
Join Medium for free to get updates from this writer.
Comparison sort algorithms that occur often in interviews include: Bubble Sort, Selection Sort, Insert Sort, Merge Sort, Quick Sort.
- Bubble Sort: compare any two adjacent numbers, if the previous one is bigger than the latter one, switch these two numbers.
Time complexity: O(n²), stability
- Selection Sort: find the minimum number in each iteration and put it in at first. Starting with the second position for the next iteration.
Time complexity: O(n²), unstability
- Insert Sort: on an already sorted array, insert a number each time. Start with the first element of the array for each iteration.
Time complexity: best case is O(n), worst case is O(n²), stability
- Merge Sort: make each subarray sorted first internally and then make the subarrays sorted.
Time complexity:O(nlogn), stability
- Quick Sort: Pick the number in the mid position as the baseline. Iterate through the array. If a number is less than the baseline, put it on the left of the baseline number. Otherwise, put it on the right. Then recurse on the left and right part.
Time complexity: average O(nlogn),worst O(n²), unstability







