The Ω(n log n) Sorting Lower Bound
Decision trees, log₂ n!, and lower bounds for other problems by reduction.
Counting the leaves of a decision tree proves that no comparison sort can always sort 12 keys with fewer than 29 comparisons. The true minimum is 30, and settling the minimum for 16 keys took until 2022 and a large computer search, which found 46, not the 45 that Knuth had conjectured.
What you’ll learn
- Model comparison sorting as a binary decision tree, and prove the worst-case bound and the same bound on average, including for randomized algorithms.
- Estimate with Stirling's formula, and extend the bound to inputs with repeated keys, where it becomes an entropy.
- Describe merge insertion (Ford–Johnson) and the known exact minimum numbers of comparisons .
- Carry lower bounds to element distinctness, closest pair and convex hull by reduction, and state the models in which they hold.
Decision trees and the counting bound
Different orders need different leaves. Take two inputs of distinct keys whose relative orders differ. If both reached the same leaf, the algorithm would apply the same permutation to both, and at most one of the results could be sorted. So the orders reach different leaves:
Height. A binary tree of height has at most leaves:
Size of the bound, crudely. The largest factors of are each at least , which already gives without Stirling:
Size of the bound, precisely. Stirling's formula gives the second-order term : for , , while .
What is , the counting bound for sorting 5 keys in the worst case?
Show the answer
7
and , so at least 7 comparisons. Seven also suffice, a classic puzzle that merge insertion solves; merge sort and binary insertion sort both need 8 in the worst case.
On average, with coins, and with repeated keys
A worst-case bound leaves room for algorithms that are usually fast. There is none: the bound holds on average over random inputs, and therefore also for randomized algorithms on their worst input.
Kraft's inequality. The leaves of a binary tree, at depths , satisfy the inequality below: give the root weight 1 and split every node's weight equally between its two children, so the leaves end with weights that sum to at most 1.
Jensen. Since is convex, the average of is at least 2 raised to minus the average depth:
Average case. On a uniformly random order of distinct keys, each of the orders ends at its own leaf, so the expected number of comparisons of any deterministic comparison sort is at least .
Randomized algorithms. Fixing the coin flips turns a randomized sort into a probability distribution over deterministic trees. Each tree averages at least on a random input, so the mixture does too, and some input must cost at least the average (this averaging step is Yao's principle). Randomized quicksort's is within a constant factor of optimal, and no randomness can do better than .
The entropy bound is achievable up to a constant factor: quicksort with three-way partitioning comes within a constant factor of the lower bound on random multisets with many duplicates, as Sebastian Wild proved in 2018, resolving a conjecture of Sedgewick and Bentley.
Information can also come from outside the comparisons. If the input is known in advance to be one of only possible orders, for example because some comparisons are already known, the same counting gives , and Fredman (1976) showed that comparisons always suffice. Finding those comparisons efficiently is harder; for inputs constrained by a known partial order, Kahn and Kim (1995) gave a polynomial-time method using comparisons.
The keys take only two values, each exactly times. What does the multinomial bound say a binary decision tree needs in the worst case?
- About comparisons, as for distinct keys
- About comparisons
- About comparisons:
- Nothing: the bound needs distinct keys
Show the answer
C. About comparisons:
There are arrangements and , the entropy bound with bit per key. A linear number of comparisons is also enough, so here sorting is linear even in the comparison model.
The exact minimum: merge insertion
Let be the least number of comparisons that sorts every order of distinct keys: the height of the shortest decision tree. The counting bound says . Merge sort and binary insertion sort both make comparisons in the worst case, which exceeds the bound already for (8 against 7). The algorithm that comes closest was found in 1959.
The strange order is the whole trick. When is inserted, the chain below holds exactly keys, and binary search over keys needs exactly comparisons with nothing wasted; the later keys of the group have no larger ranges. Knuth's closed form for the worst case, checked here against an implementation by brute force over all inputs for :
| merge sort, binary insertion | merge insertion | true minimum | ||
|---|---|---|---|---|
| 5 | 7 | 8 | 7 | 7 |
| 10 | 22 | 25 | 22 | 22 |
| 12 | 29 | 33 | 30 | 30 |
| 16 | 45 | 49 | 46 | 46 |
| 20 | 62 | 69 | 62 | 62 |
| 22 | 70 | 79 | 71 | 71 |
For large , writing , the coefficient oscillates between about 1.32 and 1.415, while the counting bound has . Merge insertion is not always optimal, either: Manacher (1979) showed that it can be beaten for some larger .
Merge insertion sorts 22 keys with 71 comparisons in the worst case, one more than . What does that fact alone establish about ?
- , because merge insertion is optimal
- Only ; excluding 70 needs a separate argument, which for 22 keys was a computer-assisted proof
- , because the counting bound is always attained
- Nothing, because the counting bound does not apply to 22 keys
Show the answer
B. Only ; excluding 70 needs a separate argument, which for 22 keys was a computer-assisted proof
An algorithm gives an upper bound and counting gives a lower bound. Closing the gap of one comparison means showing that no decision tree of height 70 sorts 22 keys, which requires searching or reasoning over the possible trees; it has been done, so is known.
Lower bounds by reduction, and their models
A yes/no problem has only two kinds of leaves, so counting leaves proves nothing directly. Element distinctness, "are the keys all different?", still needs comparisons, by an adversary argument:
Adjacent keys must meet. Run a correct algorithm on distinct keys . If it never compares some adjacent pair , change to equal : no key lies strictly between them, so every comparison gives the same answer as before, and the algorithm still says "distinct". Contradiction, so on every distinct input:
Different orders, different leaves. The answers to those adjacent comparisons fix the order. Two distinct inputs with different orders therefore cannot follow the same path, so the "distinct" answer alone needs leaves:
| Problem | Reduction | Lower bound |
|---|---|---|
| Element distinctness | the adversary above; Ben-Or's components | , comparisons and algebraic trees |
| Closest pair of points | from distinctness: some pair is at distance 0 iff two points coincide | , algebraic trees |
| Convex hull, vertices in order | from sorting: the points lie on a parabola, so the hull lists them in order of | , algebraic trees |
| Sorting reals | distinctness reduces to it: sort, then compare neighbors | , algebraic trees |
Key takeaways
- A comparison sort is a binary decision tree with at least leaves, so it needs comparisons in the worst case.
- Kraft's inequality makes the same bound hold on average over random inputs, and Yao's averaging argument makes it hold in expectation for randomized algorithms.
- With repeated keys the bound becomes , and three-way quicksort is within a constant factor of it.
- Merge insertion is optimal for every ; the counting bound is exact only for , and , and were settled in 2022.
- Reductions carry to element distinctness, closest pair and convex hull, but only in comparison or algebraic models; hashing and bit tricks escape them.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A decision tree sorts every order of 4 distinct keys. What is the smallest height it can have?
- 4
- 5
- 6
- 24
Show the answer
B. 5
It needs leaves, and , so the height is at least 5. Five comparisons suffice: compare with and with , say and , then compare the winners, say . Now and , so belongs in one of three gaps below , and a binary search over finds it with 2 more comparisons.
Can a randomized comparison sort guarantee fewer than expected comparisons on every input?
- Yes: random pivots defeat every fixed bad input
- No: averaged over a uniformly random input, every deterministic tree in its support needs at least , so some input costs at least that in expectation
- Yes, if it uses enough random bits
- Only if the keys are integers
Show the answer
B. No: averaged over a uniformly random input, every deterministic tree in its support needs at least , so some input costs at least that in expectation
A randomized algorithm is a distribution over decision trees. Each has average depth at least over the orders (Kraft and Jensen), so the average over inputs and coins is at least , and the worst input is at least the average.
To show that computing the convex hull of points, with its vertices listed in order, needs time in the algebraic decision tree model, you…
- reduce convex hull to sorting: sort the points by angle
- reduce sorting to convex hull: map each number to the point , whose hull lists all the points in order of
- count the hulls: there are possible hulls
- note that the hull has at most vertices
Show the answer
B. reduce sorting to convex hull: map each number to the point , whose hull lists all the points in order of
All the lifted points lie on a parabola, so every one is a hull vertex, and walking the hull lists the in sorted order. A fast hull algorithm would therefore sort fast, and sorting reals needs in this model. A lower bound transfers from the problem you reduce from.
Someone claims an expected-time algorithm that decides whether 64-bit integers are all distinct. Does this contradict the lower bound for element distinctness?
- Yes, so the claim must be wrong
- No: hashing uses the bits of the keys, which the comparison and algebraic decision tree models forbid
- No, because the lower bound is only for the worst case
- No, because 64-bit integers are bounded, so is at most
Show the answer
B. No: hashing uses the bits of the keys, which the comparison and algebraic decision tree models forbid
Ben-Or's bound covers algorithms that only compare keys or test signs of polynomials in them. Hashing on the word RAM treats keys as bit strings and escapes the model, which is why hash sets find duplicates in expected time.
Further reading
- D. E. Knuth, The Art of Computer Programming, Vol. 3: Sorting and Searching, 2nd ed. (Addison-Wesley, 1998), §5.3.1.
- L. R. Ford Jr. & S. M. Johnson, "A tournament problem," American Mathematical Monthly 66(5) (1959), 387–389.
- F. Stober & A. Weiß, "Lower bounds for sorting 16, 17, and 18 elements," arXiv:2206.05597 (2022).
- M. Ben-Or, "Lower bounds for algebraic computation trees," Proceedings of the 15th ACM Symposium on Theory of Computing (1983).
- S. Wild, "Quicksort is optimal for many equal keys," Proceedings of ANALCO (2018); arXiv:1608.04906.