Simple Algorithm
Algorithms & Data Structures · Optimal · 28 min

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 ⌈log⁡2n!⌉\lceil \log_2 n! \rceil and the same bound on average, including for randomized algorithms.
  • Estimate log⁡2n!\log_2 n! 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 S(n)S(n).
  • 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

DerivationProof
  1. #leaves  ≥  n!\#\text{leaves} \;\ge\; n!

    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 n!n! orders reach n!n! different leaves:

  2. 2h  ≥  n!⟹h  ≥  ⌈log⁡2n!⌉2^h \;\ge\; n! \quad\Longrightarrow\quad h \;\ge\; \lceil \log_2 n! \rceil

    Height. A binary tree of height hh has at most 2h2^h leaves:

  3. n!  ≥  (n/2)n/2⟹log⁡2n!  ≥  n2log⁡2n2n! \;\ge\; (n/2)^{n/2} \quad\Longrightarrow\quad \log_2 n! \;\ge\; \tfrac{n}{2} \log_2 \tfrac{n}{2}

    Size of the bound, crudely. The largest n/2n/2 factors of n!n! are each at least n/2n/2, which already gives Ω(nlog⁡n)\Omega(n \log n) without Stirling:

  4. log⁡2n!=nlog⁡2n−nlog⁡2e+12log⁡2(2πn)+O(1/n)\log_2 n! = n \log_2 n - n \log_2 e + \tfrac12 \log_2(2\pi n) + O(1/n)

    Size of the bound, precisely. Stirling's formula n!=2πn (n/e)n(1+O(1/n))n! = \sqrt{2\pi n}\,(n/e)^n (1 + O(1/n)) gives the second-order term −nlog⁡2e-n \log_2 e: for n=106n = 10^6, log⁡2n!≈1.849×107\log_2 n! \approx 1.849 \times 10^7, while nlog⁡2n≈1.993×107n \log_2 n \approx 1.993 \times 10^7.

Predict before you look

What is ⌈log⁡25!⌉\lceil \log_2 5! \rceil, the counting bound for sorting 5 keys in the worst case?

Show the answer

7

5!=1205! = 120 and 26=64<120≤128=272^6 = 64 < 120 \le 128 = 2^7, 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.

DerivationAverage depth is at least log₂ N
  1. ∑ℓ=1N2−dℓ  ≤  1\sum_{\ell=1}^{N} 2^{-d_\ell} \;\le\; 1

    Kraft's inequality. The leaves of a binary tree, at depths d1,…,dNd_1, \dots, d_N, 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 2−dℓ2^{-d_\ell} that sum to at most 1.

  2. 2−dˉ  ≤  1N∑ℓ2−dℓ  ≤  1N⟹dˉ  ≥  log⁡2N2^{-\bar d} \;\le\; \frac1N \sum_{\ell} 2^{-d_\ell} \;\le\; \frac1N \quad\Longrightarrow\quad \bar d \;\ge\; \log_2 N

    Jensen. Since x↦2−xx \mapsto 2^{-x} is convex, the average of 2−dℓ2^{-d_\ell} is at least 2 raised to minus the average depth:

  3. Erandom input[ C ]  ≥  log⁡2n!\E_{\text{random input}}[\,C\,] \;\ge\; \log_2 n!

    Average case. On a uniformly random order of distinct keys, each of the n!n! orders ends at its own leaf, so the expected number of comparisons of any deterministic comparison sort is at least log⁡2n!\log_2 n!.

  4. max⁡inputEcoins[ C ]  ≥  Einput Ecoins[ C ]  ≥  log⁡2n!\max_{\text{input}} \E_{\text{coins}}[\,C\,] \;\ge\; \E_{\text{input}}\,\E_{\text{coins}}[\,C\,] \;\ge\; \log_2 n!

    Randomized algorithms. Fixing the coin flips turns a randomized sort into a probability distribution over deterministic trees. Each tree averages at least log⁡2n!\log_2 n! 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 1.39 nlog⁡2n1.39\,n \log_2 n is within a constant factor of optimal, and no randomness can do better than log⁡2n!\log_2 n!.

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 Γ\Gamma possible orders, for example because some comparisons are already known, the same counting gives ⌈log⁡2Γ⌉\lceil \log_2 \Gamma \rceil, and Fredman (1976) showed that log⁡2Γ+2n\log_2 \Gamma + 2n 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 O(log⁡Γ)O(\log \Gamma) comparisons.

Check your understanding

The nn keys take only two values, each exactly n/2n/2 times. What does the multinomial bound say a binary decision tree needs in the worst case?

  1. About nlog⁡2nn \log_2 n comparisons, as for distinct keys
  2. About log⁡2n\log_2 n comparisons
  3. About nn comparisons: log⁡2(nn/2)=n−12log⁡2n−O(1)\log_2 \binom{n}{n/2} = n - \tfrac12 \log_2 n - O(1)
  4. Nothing: the bound needs distinct keys
Show the answer

C. About nn comparisons: log⁡2(nn/2)=n−12log⁡2n−O(1)\log_2 \binom{n}{n/2} = n - \tfrac12 \log_2 n - O(1)

There are (nn/2)\binom{n}{n/2} arrangements and log⁡2(nn/2)≈n\log_2 \binom{n}{n/2} \approx n, the entropy bound with H=1H = 1 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 S(n)S(n) be the least number of comparisons that sorts every order of nn distinct keys: the height of the shortest decision tree. The counting bound says S(n)≥⌈log⁡2n!⌉S(n) \ge \lceil \log_2 n! \rceil. Merge sort and binary insertion sort both make ∑k=1n⌈log⁡2k⌉\sum_{k=1}^{n} \lceil \log_2 k \rceil comparisons in the worst case, which exceeds the bound already for n=5n = 5 (8 against 7). The algorithm that comes closest was found in 1959.

The strange order is the whole trick. When btkb_{t_k} is inserted, the chain below atka_{t_k} holds exactly tk+tk−1−1=2k−1t_k + t_{k-1} - 1 = 2^k - 1 keys, and binary search over 2k−12^k - 1 keys needs exactly kk 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 n≤8n \le 8:

F(n)  =  ∑k=1n⌈log⁡23k4⌉F(n) \;=\; \sum_{k=1}^{n} \left\lceil \log_2 \tfrac{3k}{4} \right\rceil
The worst-case comparisons of merge insertion.
nn⌈log⁡2n!⌉\lceil \log_2 n! \rceilmerge sort, binary insertionmerge insertion F(n)F(n)true minimum S(n)S(n)
57877
1022252222
1229333030
1645494646
2062696262
2270797171
The counting bound is exact for n≤11n \le 11, 2020 and 2121, where merge insertion meets it. That S(12)=30S(12) = 30 was shown by Wells (1965) with a computer search. S(16),S(17),S(18)=46,50,54S(16), S(17), S(18) = 46, 50, 54 were settled by Stober and Weiß (2022), disproving Knuth's conjecture that S(16)=45S(16) = 45. Every S(n)S(n) with n≤22n \le 22 is now known, and merge insertion attains all of them.

For large nn, writing F(n)=nlog⁡2n−c(n) n+O(log⁡n)F(n) = n \log_2 n - c(n)\,n + O(\log n), the coefficient c(n)c(n) oscillates between about 1.32 and 1.415, while the counting bound has log⁡2e≈1.443\log_2 e \approx 1.443. Merge insertion is not always optimal, either: Manacher (1979) showed that it can be beaten for some larger nn.

Check your understanding

Merge insertion sorts 22 keys with 71 comparisons in the worst case, one more than ⌈log⁡222!⌉=70\lceil \log_2 22! \rceil = 70. What does that fact alone establish about S(22)S(22)?

  1. S(22)=71S(22) = 71, because merge insertion is optimal
  2. Only 70≤S(22)≤7170 \le S(22) \le 71; excluding 70 needs a separate argument, which for 22 keys was a computer-assisted proof
  3. S(22)=70S(22) = 70, because the counting bound is always attained
  4. Nothing, because the counting bound does not apply to 22 keys
Show the answer

B. Only 70≤S(22)≤7170 \le S(22) \le 71; 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 S(22)=71S(22) = 71 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 nn keys all different?", still needs log⁡2n!\log_2 n! comparisons, by an adversary argument:

DerivationElement distinctness needs log₂ n! comparisons
  1. every pair (zi,zi+1) is compared on the path of the input\text{every pair } (z_i, z_{i+1}) \text{ is compared on the path of the input}

    Adjacent keys must meet. Run a correct algorithm on distinct keys z1<⋯<znz_1 < \dots < z_n. If it never compares some adjacent pair zi,zi+1z_i, z_{i+1}, change zi+1z_{i+1} to equal ziz_i: 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:

  2. height  ≥  ⌈log⁡2n!⌉\text{height} \;\ge\; \lceil \log_2 n! \rceil

    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 n!n! leaves:

ProblemReductionLower bound
Element distinctnessthe adversary above; Ben-Or's componentsΩ(nlog⁡n)\Omega(n \log n), comparisons and algebraic trees
Closest pair of pointsfrom distinctness: some pair is at distance 0 iff two points coincideΩ(nlog⁡n)\Omega(n \log n), algebraic trees
Convex hull, vertices in orderfrom sorting: the points (xi,xi2)(x_i, x_i^2) lie on a parabola, so the hull lists them in order of xxΩ(nlog⁡n)\Omega(n \log n), algebraic trees
Sorting realsdistinctness reduces to it: sort, then compare neighborsΩ(nlog⁡n)\Omega(n \log n), algebraic trees
A reduction from AA to BB that costs o(nlog⁡n)o(n \log n) means a fast algorithm for BB would solve AA fast, so BB inherits AA's lower bound. The direction matters.

Key takeaways

  • A comparison sort is a binary decision tree with at least n!n! leaves, so it needs ⌈log⁡2n!⌉=nlog⁡2n−1.443 n+O(log⁡n)\lceil \log_2 n! \rceil = n \log_2 n - 1.443\,n + O(\log n) 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 log⁡2n!n1!⋯nk!≈nH\log_2 \frac{n!}{n_1! \cdots n_k!} \approx nH, and three-way quicksort is within a constant factor of it.
  • Merge insertion is optimal for every n≤22n \le 22; the counting bound is exact only for n≤11n \le 11, 2020 and 2121, and S(16),S(17),S(18)S(16), S(17), S(18) were settled in 2022.
  • Reductions carry Ω(nlog⁡n)\Omega(n \log n) 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.

Question 1

A decision tree sorts every order of 4 distinct keys. What is the smallest height it can have?

  1. 4
  2. 5
  3. 6
  4. 24
Show the answer

B. 5

It needs 4!=244! = 24 leaves, and 24=16<24≤32=252^4 = 16 < 24 \le 32 = 2^5, so the height is at least 5. Five comparisons suffice: compare aa with bb and cc with dd, say a<ba < b and c<dc < d, then compare the winners, say b<db < d. Now a<b<da < b < d and c<dc < d, so cc belongs in one of three gaps below dd, and a binary search over a,ba, b finds it with 2 more comparisons.

Question 2

Can a randomized comparison sort guarantee fewer than log⁡2n!\log_2 n! expected comparisons on every input?

  1. Yes: random pivots defeat every fixed bad input
  2. No: averaged over a uniformly random input, every deterministic tree in its support needs at least log⁡2n!\log_2 n!, so some input costs at least that in expectation
  3. Yes, if it uses enough random bits
  4. 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 log⁡2n!\log_2 n!, so some input costs at least that in expectation

A randomized algorithm is a distribution over decision trees. Each has average depth at least log⁡2n!\log_2 n! over the n!n! orders (Kraft and Jensen), so the average over inputs and coins is at least log⁡2n!\log_2 n!, and the worst input is at least the average.

Question 3

To show that computing the convex hull of nn points, with its vertices listed in order, needs Ω(nlog⁡n)\Omega(n \log n) time in the algebraic decision tree model, you…

  1. reduce convex hull to sorting: sort the points by angle
  2. reduce sorting to convex hull: map each number xix_i to the point (xi,xi2)(x_i, x_i^2), whose hull lists all the points in order of xx
  3. count the hulls: there are n!n! possible hulls
  4. note that the hull has at most nn vertices
Show the answer

B. reduce sorting to convex hull: map each number xix_i to the point (xi,xi2)(x_i, x_i^2), whose hull lists all the points in order of xx

All the lifted points lie on a parabola, so every one is a hull vertex, and walking the hull lists the xix_i in sorted order. A fast hull algorithm would therefore sort fast, and sorting reals needs Ω(nlog⁡n)\Omega(n \log n) in this model. A lower bound transfers from the problem you reduce from.

Question 4

Someone claims an O(n)O(n) expected-time algorithm that decides whether nn 64-bit integers are all distinct. Does this contradict the Ω(nlog⁡n)\Omega(n \log n) lower bound for element distinctness?

  1. Yes, so the claim must be wrong
  2. No: hashing uses the bits of the keys, which the comparison and algebraic decision tree models forbid
  3. No, because the lower bound is only for the worst case
  4. No, because 64-bit integers are bounded, so nn is at most 2642^{64}
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 O(n)O(n) 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.