3.6.9Sorting & Searching

Lower bound for comparison sorts — Ω(n log n) proof via decision trees

2,106 words10 min readdifficulty · medium4 backlinks

WHAT are we even claiming?

WHY does this matter? It tells you merge sort / heapsort (O(nlogn)O(n\log n)) are asymptotically optimal — no clever comparison-only algorithm can ever beat them. It also explains WHY counting sort / radix sort (O(n)O(n)) must cheat: they use the values themselves (indexing into buckets), not just comparisons.


HOW: the decision tree model

Figure — Lower bound for comparison sorts — Ω(n log n) proof via decision trees

The derivation — from scratch

We want a lower bound on the height hh of the decision tree.

Step 1 — Every permutation needs its own leaf. There are n!n! distinct orderings of nn distinct elements. The algorithm must output the correct one for each input. If two different required permutations shared the same leaf, the algorithm would output the same answer for both — wrong for at least one.

Why this step? The leaf is the final answer; one answer per leaf, and we need every correct answer reachable. So: (number of leaves)    n!\text{(number of leaves)} \;\ge\; n!

Step 2 — A binary tree of height hh has at most 2h2^h leaves. At depth 0 there's 1 node; each level at most doubles. A tree of height hh has at most 2h2^h leaves.

Why this step? Each comparison has only 2 outcomes, so the tree is binary, and hh comparisons ⇒ at most 2h2^h distinct root-to-leaf paths.

Step 3 — Combine. 2h    (leaves)    n!h    log2(n!)2^h \;\ge\; (\text{leaves}) \;\ge\; n! \quad\Longrightarrow\quad h \;\ge\; \log_2(n!)

Step 4 — Bound log2(n!)\log_2(n!) from below. We need log2(n!)=Ω(nlogn)\log_2(n!) = \Omega(n\log n). Use the lower half of the factorial: n!=12n    n2(n2+1)nn/2 terms, eachn/2    (n2)n/2n! = 1\cdot 2 \cdots n \;\ge\; \underbrace{\frac{n}{2}\cdot\left(\frac{n}{2}+1\right)\cdots n}_{n/2 \text{ terms, each} \ge n/2} \;\ge\; \left(\frac{n}{2}\right)^{n/2}

Why this step? Throwing away the small factors (1,2,,n211,2,\dots,\tfrac n2-1) only makes the product smaller, and every remaining factor is n/2\ge n/2 — a clean, easy bound. Taking log2\log_2: log2(n!)    n2log2 ⁣n2  =  n2(log2n1)  =  Ω(nlogn).\log_2(n!) \;\ge\; \frac{n}{2}\log_2\!\frac{n}{2} \;=\; \frac{n}{2}(\log_2 n - 1) \;=\; \Omega(n\log n).


Worked examples


Common mistakes (steel-manned)


Recall Feynman: explain it to a 12-year-old

Imagine I shuffle 3 cards and you must put them in order, but you're blindfolded — you can only ask me "is this card smaller than that one?" Each question gets a yes/no. With qq questions you can tell apart at most 2q2^q different situations. But 3 cards can be shuffled 3×2×1=63\times2\times1=6 ways! Since 22=4<62^2=4 < 6, two questions aren't enough — you must ask at least 3. For nn cards there are n!n! shuffles, so you need about nlognn\log n questions. That's why no blindfold (comparison-only) sorter can be super fast. The clever sorters that are fast cheat by looking at the numbers instead of just asking comparisons.


Active-recall flashcards

What is a comparison sort?
An algorithm that determines order using only comparisons (ai<aja_i < a_j etc.), never inspecting actual key values.
What does a decision-tree leaf represent?
A specific output permutation the algorithm produces.
Why must a decision tree have n!\ge n! leaves?
There are n!n! possible input orderings; each needs a distinct correct output, so a distinct reachable leaf.
Max leaves of a binary tree of height hh?
2h2^h (each comparison has 2 outcomes).
Derive the worst-case lower bound.
2hn!hlog2(n!)=Ω(nlogn)2^h \ge n! \Rightarrow h \ge \log_2(n!) = \Omega(n\log n).
Why is log2(n!)=Ω(nlogn)\log_2(n!) = \Omega(n\log n)?
n!(n/2)n/2n! \ge (n/2)^{n/2}, so log2n!n2log2n2=Ω(nlogn)\log_2 n! \ge \frac n2\log_2\frac n2 = \Omega(n\log n).
Worst-case comparisons to sort 3 elements?
log26=3\lceil\log_2 6\rceil = 3.
Why doesn't counting sort violate the bound?
It's not comparison-based; it indexes buckets by value, so the decision-tree model doesn't apply.
Tight estimate of log2(n!)\log_2(n!) via Stirling?
nlog2nnlog2e+O(logn)nlog2n1.44nn\log_2 n - n\log_2 e + O(\log n) \approx n\log_2 n - 1.44n.
What does the height of the decision tree equal?
The worst-case number of comparisons.
Is merge sort optimal among comparison sorts?
Yes — its Θ(nlogn)\Theta(n\log n) matches the Ω(nlogn)\Omega(n\log n) lower bound.
Common confusion: log2n\log_2 n vs log2n!\log_2 n!?
We distinguish n!n! orderings (whole permutation), not locate 1 of nn items.

Connections

  • Merge Sort — achieves the O(nlogn)O(n\log n) upper bound, proving optimality.
  • Heapsort — another Θ(nlogn)\Theta(n\log n) comparison sort meeting the bound.
  • Quicksort — average O(nlogn)O(n\log n); worst case O(n2)O(n^2) but still bounded below by Ω(nlogn)\Omega(n\log n) on average.
  • Counting Sort / Radix Sort — non-comparison sorts that legally beat nlognn\log n.
  • Big-O and Asymptotic Notation — meaning of Ω\Omega.
  • Information Theory & Entropylog2(n!)\log_2(n!) bits of information must be extracted.
  • Binary Search — same "log\log of outcomes" counting idea, for nn outcomes.
  • Stirling's Approximation — used to sharpen log2(n!)\log_2(n!).

Concept Map

models as

only uses

becomes

two outcomes

each needs own

height h equals

binary tree has

combined with

gives 2^h >= n!

Stirling approx

proves optimal

explains why

Comparison sort

Decision tree

Compares a_i vs a_j

Internal node

Leaves = permutations

n! orderings

Worst-case comparisons

At most 2^h leaves

h >= log2 of n!

h = Omega of n log n

Merge sort / heapsort optimal

Counting/radix sort use values

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Socho tum blindfolded ho aur tumhe cards ko sorted order me lagana hai, par tum sirf ek hi sawaal puch sakte ho: "kya yeh card us card se chhota hai?" — yes ya no. Har comparison ek yes/no question hai. Yahi hota hai har comparison sort (merge sort, quicksort, heapsort) ke andar — wo sirf elements compare karte hain, unki actual value ko "dekhte" nahi.

Ab maths: nn elements ko n!n! tarah se arrange kiya ja sakta hai. Tumhe pata lagana hai input kaunsi arrangement hai — yani n!n! possible answers me se ek. Agar tum kk yes/no questions poochte ho, toh tum sirf 2k2^k alag-alag cases ko distinguish kar sakte ho. Sahi answer dene ke liye chahiye 2kn!2^k \ge n!, jisse klog2(n!)nlog2nk \ge \log_2(n!) \approx n\log_2 n. Isi liye worst case me koi bhi comparison sort Ω(nlogn)\Omega(n\log n) comparisons se kam nahi kar sakta.

Hum isko ek decision tree se prove karte hain: har internal node ek comparison, har leaf ek output permutation. Tree ko kam se kam n!n! leaves chahiye, aur binary tree of height hh me max 2h2^h leaves hote hain — bas isi se result nikal aata hai. Iska matlab merge sort already optimal hai, usse fast comparison-based kuch nahi ho sakta.

Aur ek important baat: counting sort / radix sort jo O(n)O(n) me chalte hain, wo is bound ko todte nahi — kyunki wo comparison sort hain hi nahi! Wo values ko directly array index ki tarah use karte hain. Toh rule yaad rakho: bound sirf "comparison-only" algorithms par lagta hai.

Go deeper — visual, from zero

Test yourself — Sorting & Searching

Connections