№ 50 · computing

Why no comparison sort can beat n log n

Every sorting algorithm that works by comparing two items at a time has a floor under it. Nothing gets below about n log2 n comparisons in the worst case. The proof is a counting argument that fits on a napkin.

What the claim is

A comparison sort learns about items only by comparing two of them; it never looks at a number's digits or uses its value as an address. MIT's 6.006 calls this the comparison model: items are black boxes, and the cost is the number of comparisons. In this model, sorting n items needs at least log2(n!) comparisons in the worst case, and log2(n!) grows like n log2 n.

Why it matters

Most claims about algorithms are upper bounds: "here is a fast way." Erickson's lecture notes on lower bounds call this bragging about how easy a problem is. A lower bound is the rarer, opposite statement: it holds for every algorithm, including ones nobody has invented. Merge sort and heap sort already reach n log n, so in the comparison model they are optimal.

Interactive Pick n and press sort: a fresh random array is sorted twice, by insertion sort and by merge sort, and every comparison is counted as it happens — watch where the two tallies land against the log2(n!) floor.

Both sorts run on the same freshly shuffled array of n distinct numbers, and the tally counts every time the code asks whether one item is larger than another — nothing else. The blue line is ⌈log2(n!)⌉, computed from the exact integer n!; no comparison sort can use fewer than this on every input, though a single lucky run may dip under it, since the floor is a worst-case statement. The dashed line is n log2 n. Merge sort's dots track the floor to within a small factor; insertion sort's climb away from it because its comparison count grows with n2. The vertical axis is logarithmic so both can be seen at once. Faint dots are earlier runs.

The napkin argument

Draw every possible run of a comparison sort as a tree: each internal node is one comparison with two branches for its outcomes, each leaf an announced sorted order. A run is a root-to-leaf path; the worst case is the longest one — the tree's height. This is a decision tree, and both sources build the bound on it.

Now count leaves. The answer is a permutation of the n items, and any of the n! permutations could be right for some input. If two shared a leaf, the algorithm would answer both the same and be wrong for one. So the tree needs at least n! leaves.

A binary tree of height h has at most 2h leaves. So 2h ≥ n!, and h ≥ log2(n!). That is the whole proof. Only the number of possible answers fixes the floor, not how smart the algorithm is.

How big is log2(n!)?

Since n! = 1 × 2 × … × n, its logarithm is log2 1 + log2 2 + … + log2 n. The 6.006 notes estimate it by dropping the first half of the terms; each remaining n/2 is at least log2(n/2), so the sum is at least (n/2) log2(n/2). Stirling's formula sharpens this to log2(n!) ≈ n log2 n − (log2 e) n, with log2 e about 1.44. Erickson states the result as: the decision-tree complexity of sorting is Θ(n log n).

The escape hatch

The bound is about a model, and Erickson is blunt that choosing the model is what makes a lower bound meaningful. Bubble, insertion, quick, heap and merge sort all fit the comparison model: they treat two inputs identically whenever every comparison comes out the same way. Radix sort and bucket sort do not. Counting sort takes integer keys from 0 to k−1 and uses each key as an index into an array of k lists — it never asks which of two items is larger. That question has far more than two outcomes, so the decision-tree count does not apply, and counting sort runs in time proportional to n + k. The floor is not broken; it is simply not in the room.

In short

A comparison sort learns one bit per question and must distinguish n! answers. That takes at least log2(n!) questions, roughly n log2 n. Merge sort meets the floor within a constant factor. Algorithms that beat it do so by not comparing at all.

Where this comes from

  1. 6.006 Introduction to Algorithms, Fall 2011, Lecture 7: Linear-Time Sorting linked only, not reproduced
    MIT OpenCourseWare · 2011
    ocw.mit.edu/courses/6-006-introduction-to-algorithms-fall-2011/bf7d79105762bf79bbc0925438e1468a_MIT6_006F11_lec07.pdf
  2. Algorithms, Lecture 12: Lower Bounds linked only, not reproduced
    Jeff Erickson · 2018
    jeffe.cs.illinois.edu/teaching/algorithms/notes/12-lowerbounds.pdf