Introduction
This note brings the sorting algorithms together. It proves that no comparison sort can beat in the worst case (or on average), states the exact comparison counts for related problems (maximum, minimum and maximum, second largest, merging), and compares all standard sorts on time, space, stability and adaptivity. It is the reference page to use when a question asks "which sort" or "which statement is true".
Prerequisites
- Elementary Sorting, Merge Sort, Quicksort, Heapsort and Linear-Time Sorting: the algorithms being compared.
- Trees and Binary Trees: a binary tree of height has at most leaves.
The decision-tree model
A comparison sort gains information about the input only by comparing pairs of keys (?). Its behaviour on all inputs of size can be drawn as a decision tree:
- each internal node is a comparison , with one subtree for each outcome;
- each leaf is a permutation , meaning "the sorted order is ";
- one execution follows one root-to-leaf path; its number of comparisons is the length of that path.
The model counts only comparisons and ignores all other work, so a lower bound on comparisons is a lower bound on time.

The lower bound
Theorem (CLRS 3e, Theorem 8.1). Any comparison sort algorithm requires comparisons in the worst case.
Proof. The algorithm must be correct for each of the orderings of distinct keys, and different orderings need different leaves, so the tree has at least reachable leaves. A binary tree of height has at most leaves. Therefore , so
where follows from Stirling's approximation (or from ).
Consequences:
- Merge sort and heapsort, with worst-case comparisons, are asymptotically optimal comparison sorts.
- The exact bound is 1 for , 3 for , 5 for , 7 for and 10 for . For these small it is achieved, so it is the minimum worst-case number of comparisons. In general it is only a lower bound: no theorem says some algorithm meets it for every .
- The best case is at least . Every leaf of a correct decision tree is at depth at least . Think of each comparison as an edge between two keys: if the edges seen on a path do not connect all keys, two of the resulting groups have never been compared, and the algorithm cannot know their relative order. Insertion sort on sorted input meets this with exactly comparisons; in Figure 1 the shortest paths have length .
- Nearly sorted input. For , if every key is at most positions from its sorted place, a min-heap of the next at most keys sorts in time. For a lower bound, permute keys independently inside full blocks of consecutive sorted ranks, leaving the remaining keys fixed. All permutations satisfy the displacement limit, requiring at least worst-case comparisons, which is . For , the input is already sorted; reading or outputting it takes , and no comparison lower bound of that order is implied.
- Average case too. The average depth of the leaves of any binary tree is at least , so the average number of comparisons, over uniformly random orderings, is also . Quicksort's average is therefore optimal in order.
- The bound applies only to comparison sorts. Counting, radix and bucket sort avoid it by using key values as indices.
The comparison table
= number of keys; = key range; = number of digits; = number of inversions. Space is auxiliary space.
| Algorithm | Best | Average | Worst | Space | Stable | In place | Adaptive |
|---|---|---|---|---|---|---|---|
| Bubble sort (with flag) | yes | yes | yes | ||||
| Selection sort | no | yes | no | ||||
| Insertion sort | yes | yes | yes, | ||||
| Merge sort | yes | no | no (natural variant: yes) | ||||
| Quicksort | to stack | no | yes | no | |||
| Heapsort | (distinct keys) | no | yes | no | |||
| Counting sort | yes | no | no | ||||
| Radix sort (LSD) | yes | no | no | ||||
| Bucket sort | expected (uniform keys) | yes | no | no |
Number of exchanges: selection sort makes at most ; bubble sort makes exactly ; insertion sort makes shifts; heapsort makes in general.
Choosing a sort
| Situation | Good choice | Reason |
|---|---|---|
| Small , or nearly sorted input | insertion sort | , tiny overhead |
| Guaranteed with extra space | heapsort | in place with that worst case among the standard implementations in this table |
| Stability needed with worst case | merge sort | stable and optimal |
| Common efficient in-memory choice for arrays | quicksort (randomized or median-of-three) | small constants, in place |
| Linked list | merge sort | no random access needed |
| Writes are expensive | selection sort | at most exchanges |
| Integer keys in a small range | counting sort | |
| Fixed-length integer or string keys | radix sort | |
| Data larger than memory | external merge sort | sequential passes |
Important properties and observations
- Stable sorts in the table. bubble, insertion, merge, counting, radix, bucket. Unstable: selection (array form), quicksort, heapsort. Any sort can be made stable by sorting pairs , at the cost of extra space.
- In place. bubble, selection, insertion, heapsort, and quicksort (stack excepted). Merge sort on arrays and the three linear-time sorts are not.
- Already sorted input. insertion sort and flagged bubble sort take ; quicksort with a first or last pivot takes ; merge sort stays ; heapsort does too for distinct keys, but duplicate-heavy sorted inputs can be faster; selection sort stays .
- All keys equal. insertion and bubble ; heapsort ; Lomuto quicksort ; three-way quicksort .
- Lower bound statements. "every comparison sort takes time on some input" is true; "every comparison sort takes time on every input" is false (insertion sort on sorted input takes ).
GATE examination perspective
- "Which sort" questions. match the stated constraint (stability, space, worst case, nearly sorted input) with the table above.
- Counting questions. minimum comparisons for max, min-and-max, second largest, merging, or sorting a few keys; use the exact formulas and check the parity of .
- Lower-bound reasoning. decision tree height ; every leaf depth (best case of any comparison sort); for inputs whose keys are within of their place; for partial problems, count the outcomes the algorithm must distinguish (for example, possible interleavings when merging, giving at least comparisons).
- Traps. heapsort's best case on distinct keys is still ; quicksort is in place but not stable; merge sort is stable but not in place; the bound is a worst-case and average-case bound, not a best-case one.
Summary
Sources
- T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009: Section 8.1 (lower bounds for sorting, decision trees, Theorem 8.1), Section 9.1 (minimum and maximum) and Exercise 9.1-1 (the second smallest element in n + ⌈ lg n ⌉ - 2 comparisons).