The Comparison-Based Sorting Lower Bound
Statement
Any comparison-based sorting algorithm must perform comparisons in the worst case to sort elements.
A comparison-based sort uses only pairwise comparisons ( or ) to determine the relative order of elements. It makes no assumptions about the values (no counting, no hashing, no radix).
Proof via Decision Trees
A decision tree models the behaviour of any comparison-based sort:
- Each internal node represents a comparison , with two outgoing edges (Yes/No).
- Each leaf represents a specific permutation of the input — the sorted output.
- A path from root to leaf corresponds to the sequence of comparisons made on some input.
For distinct elements, there are possible input permutations. Since the algorithm must be correct for all permutations, the decision tree must have at least leaves.
A binary tree of height has at most leaves. Therefore:
Taking logarithms:
Using Stirling’s approximation ():
The height is the number of comparisons on the longest path, so the worst-case number of comparisons is .
Implications
- MergeSort at is asymptotically optimal amongst comparison sorts.
- HeapSort at is also optimal.
- QuickSort averages but can degrade to .
- No comparison sort can beat in the worst case.
Lower Bound on Swaps
A separate result: the worst-case number of swaps is . Consider the input . Every item is out of position, and each swap moves at most two items to their correct positions. Therefore at least swaps are required. Selection Sort achieves this bound with swaps.
Comparison Sorts vs Linear-Time Sorts
The bound applies only to comparison-based sorts. Algorithms that exploit properties of the data can beat this bound:
| Sort | Bound | Assumption |
|---|---|---|
| Counting Sort | Keys in range | |
| Radix Sort | -digit numbers | |
| Bucket Sort | expected | Uniform distribution over |
These are not comparison-based; they use counting, digit extraction, or hashing.
Summary
| Concept | Detail |
|---|---|
| Lower bound | comparisons |
| Proof technique | Decision tree model |
| Key inequality | |
| Stirling’s approx | |
| Optimal sorts | MergeSort, HeapSort (comparison-based) |
| Swap lower bound | |
| Exceptions | Linear-time non-comparison sorts |