Sorting rearranges n records so that their keys are in non-decreasing order. Bubble sort, selection sort and insertion sort are the three simple quadratic methods. Their worst-case quadratic cost is slower than merge sort or heapsort on large inputs, but they are short and in place.
8 min readQuick revision
Merge sort is the standard example of divide and conquer. It splits the array into two halves, sorts each half recursively, and merges the two sorted halves in linear time. Its worst-case running time is Θ(n lg n), which matches the lower bound for comparison sorting, and it is stable.
7 min readQuick revision
Quicksort sorts by divide and conquer, but puts the work in the divide step. It chooses a pivot, partitions the array so that smaller elements come before larger ones, and then sorts the two parts recursively; no combine step is needed.
9 min readQuick revision
Heapsort sorts an array by first arranging it into a max-heap and then repeatedly moving the maximum to the end. It is the only standard comparison sort that is both in place (O(1) auxiliary space) and guaranteed O(n lg n) in the worst case.
8 min readQuick revision
Every comparison sort needs Ω(n lg n) comparisons in the worst case (see Comparison of Sorting Algorithms). The three algorithms in this note beat that bound because they are not comparison sorts: they use the keys as array indices or split them into digits.
7 min readQuick revision
Brings the sorting algorithms together: the lower bound that no comparison sort can beat, exact comparison counts for related problems, and a comparison of all standard sorts on time, space, stability and adaptivity.
7 min readQuick revision