3.6.9 · D4Sorting & Searching

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

2,889 words13 min readBack to topic

Level 1 — Recognition

(Can you spot what the model says and where it applies?)

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

A comparison sort decides order using only questions of the form "is ?" — never reading the numeric value of a key.

  • (a) Bubble sort — yes. It swaps adjacent items based on a comparison.
  • (b) Counting sort — no. It does count[x]++, using the value as an array index.
  • (c) Heapsort — yes. Sift-up/sift-down compare children to parents.
  • (d) Radix sort — no. It buckets by digit value, not by comparison.
  • (e) Quicksort — yes. Partitioning compares each element to a pivot.

Rule of thumb: if it ever looks at a value (to index, hash, or arithmetic), it escapes the model. See Counting Sort and Radix Sort for the escapees, Merge Sort · Heapsort · Quicksort for the comparison crowd.

Recall Solution 1.2

(a) An internal node is a single comparison of two elements and — i.e. the question "is ?". (b) A leaf is one output permutation — the ordering the algorithm commits to once it reaches that leaf. Each internal node has 2 children, because the comparison "is ?" has exactly two outcomes (yes/, or no/). That two-ness is the whole reason the tree is binary, and why height (the longest root-to-leaf path, from the reminder above) allows at most leaves.


Level 2 — Application

(Plug numbers into .)

Recall Solution 2.1

We need enough leaves: leaves . A tree of height has at most leaves, so we need , so height 4 is impossible; is feasible. Minimum worst-case comparisons . (And indeed an optimal 4-element sort achieves exactly 5.)

Recall Solution 2.2

. So . The bound forces height . But it does not guarantee a tree of height exactly 7 exists — the bound is necessary, not automatically sufficient. (In fact, for the optimal is 7, so here they coincide, but for e.g. the true optimum exceeds .)

Recall Solution 2.3

Need leaves . With height 9: . Since , a height-9 tree can distinguish at most 512 orderings, far short of the 40320 required. The claim is impossible. You need comparisons.


Level 3 — Analysis

(Reason about the tree's shape and the bound's structure.)

Figure — Lower bound for comparison sorts — Ω(n log n) proof via decision trees
Recall Solution 3.1
  • (blue) is the true information content — the exact minimum height.
  • (yellow) is the leading term of Stirling's Approximation. The key fact inline: Stirling says , where . So the gap between the blue and yellow curves is linear, hence swallowed by the term for large : the curves hug each other.
  • (red) is the crude bound: keep only the top half of the factors of , each , so and . It sits below (we threw factors away) but still grows as a constant times .

"" (from the reminder: a lower bound up to a constant factor) only asks for some positive constant with for large . The crude red curve already delivers such a (roughly ). So even the wasteful bound proves the theorem — we never needed Stirling for the lower bound, only for the sharp constant.

Recall Solution 3.2

False, in general. A complete balanced tree of height has exactly leaves, yes. But a valid sorting tree needs exactly leaves, and is almost never a power of 2 (only ). So the minimum-height tree cannot be perfectly balanced — some root-to-leaf paths are shorter than . The bound uses only the inequality leaves ; it never claims the tree is full — the "used , not " subtlety.

Recall Solution 3.3

A ternary tree of height has at most leaves. Combining with leaves : Since is a constant, (recall ignores constant factors). The asymptotic bound is unchanged — only the constant shrinks by a factor . Any fixed number of outcomes per node gives the same .


Level 4 — Synthesis

(Combine the model with other ideas.)

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

Model the search as a binary decision tree where each internal node compares the target to some array element. Now the possible outcomes are the positions the target could occupy — there are of them (before element 1, between 1 and 2, …, after element ). So we need The difference from sorting: searching has outcomes, so questions; sorting has outcomes, so questions. Binary Search achieves , hence is optimal. This is exactly the classic "Height , not " confusion — the mistake was using the search count for the sort problem.

Recall Solution 4.2

Key fact inline (from Information Theory & Entropy): a uniform distribution over equally-likely outcomes has entropy bits — the average number of yes/no questions to identify one outcome. Here , so To output the correct permutation with certainty, the algorithm must resolve all bits. Each comparison returns one of two outcomes, giving at most 1 bit. So the number of comparisons in the worst case is — the same bound, now read as "you can't extract more bits than you ask for." For : , so bits, i.e. at least comparisons.

Recall Solution 4.3

The result is one sorted list of elements; the "unknown" is how the two lists interleave. The number of interleavings is (choose which of the output slots hold the first list's elements — see the figure above). So Using (a standard bound), . So merging needs comparisons — matching the merge step inside Merge Sort.


Level 5 — Mastery

(Build and defend a bound from scratch.)

Recall Solution 5.1

Fact (balanced-tree / Kraft bound): In any binary tree with leaves at depths , the average depth . (Intuition: a tree can't beat the perfectly balanced one, whose every leaf is at depth ; making some leaves shallower forces others deeper by more.)

Apply with . Assuming each of the inputs is equally likely, the average number of comparisons equals the average leaf depth : So even on average, comparison sorting costs . This closes the caveat that "a lucky input can finish in " — most inputs cannot; the lucky ones are a vanishing fraction.

Recall Solution 5.2

Each median query has 3 outcomes, so after queries the tree has at most leaves. We still must distinguish all orderings: Same asymptotic wall — a constant-outcome oracle can shave the constant (here by ) but never the growth. The lesson: only queries whose number of outcomes grows with the data can break the bound.

Recall Solution 5.3

Proof: (1) need leaves (one per output permutation); (2) a binary tree of height has leaves; (3) so , giving ; (4) . (a) Heapsort runs in comparisons — it meets the lower bound, so it's asymptotically optimal. (b) Radix sort isn't a comparison sort (it buckets by digit value), so the model's premise fails and it may run in without contradiction. Numbers: . , so . Crude bound . Note — the crude bound is genuinely below the true value yet still . ✔


Recall One-line summary to carry away

Count the distinct correct answers your tree must reach; take ; that's your lower bound. For sorting it's .