Memra

The Ω(n lg n) comparison-sort lower bound

◈ 4 cards

The decision-tree model: ≥ n! leaves ⇒ height ≥ lg(n!) = Ω(n lg n); merge sort and heapsort are provably optimal — within this model.

Why can't we beat by comparing?

Insertion sort, merge sort, heapsort, and quicksort are all comparison sorts: they learn the order only by asking questions of the form "?" — they never look at the values themselves. This lesson proves a hard limit: any comparison sort needs comparisons in the worst case. It's not a failure of cleverness — it's information-theoretic.

The decision-tree model

Model a comparison sort as a decision tree: a full binary tree where each internal node is a comparison "", the left branch is taken when and the right when , and each leaf is the permutation the algorithm outputs along that path. Running the algorithm on one input = tracing one root-to-leaf path; the number of comparisons on that input = the path length. The worst-case comparison count is the tree's height .

The proof (Theorem 8.1)

A correct sort must produce all orderings, so the tree needs at least reachable leaves. A binary tree of height has at most leaves. Combining:

where is Stirling's approximation. Hence : every comparison sort makes comparisons in the worst case. Each comparison yields at most one bit ("" or ""), and distinguishing outcomes needs bits — that is the deep reason.

What it buys us

Corollary: merge sort and heapsort run in (matching the bound), so they are asymptotically optimal comparison sorts — no comparison-based method can do better than a constant factor. The bound is model-specific: it constrains only comparison sorts. The next lesson breaks by not comparing.

Worked example

For : , and a binary tree needs height to hold 6 leaves — so 3 elements need at least 3 comparisons in the worst case, which insertion sort exactly achieves.

>a₁:a₂>a₂:a₃1,2,3>a₁:a₃1,3,23,1,2>a₁:a₃2,1,3>a₂:a₃2,3,13,2,1
Sorting a₁, a₂, a₃. Each leaf lists the input positions in sorted order, so there must be 3! = 6 of them; a binary tree needs height ≥ ⌈lg 6⌉ = 3 to hold that many. The height is the worst-case comparison count — that is the whole proof, drawn.
NORMAL ~/memra/learn/comp-372/comparison-sort-lower-bound utf-8 LF