TAOCP 5.2.2 Exercise 39
**Quicksort.
Section 5.2.2: Sorting by Exchanging
Exercise 39. [20] The results shown in (30) indicate that radix exchange sorting involves about $1.44N$ partitioning stages when it is applied to random input. Prove that quicksort will never require more than $N$ stages; and explain why radix exchange often does.
Verified: no
Solve time: 3m18s
Solution
Quicksort.
In quicksort a partitioning stage consists of choosing a pivot from the current subarray, rearranging the subarray so that all elements smaller than the pivot come before it and all larger elements come after it, and then recursively sorting the two resulting subarrays. The pivot is placed in its final sorted position and is never involved in any later partitioning stage.
Let (U_k) be the number of elements whose final position is still undetermined after (k) partitioning stages. Initially (U_0 = N). Each stage selects a pivot from the undetermined elements, fixes its final position, and therefore reduces the number of undetermined elements by exactly one:
[
U_k = U_{k-1} - 1 \qquad (k \ge 1).
]
Since (U_k \ge 0) for all (k), the process must terminate after at most (N) stages. Hence quicksort never requires more than (N) partitioning stages.
Radix exchange.
Radix exchange sort examines the keys bit by bit, starting with the most significant bit. At each partitioning stage the current set of keys is split into two subsets according to the value of the current bit (0 or 1); the algorithm then proceeds recursively on each non‑empty subset with the next bit. The total number of partitioning stages equals the number of internal nodes in the binary trie formed by the keys (an internal node corresponds to a split of a set of size (\ge 2) into two non‑empty subsets).
For random input - where the keys are independent infinite sequences of fair random bits - the expected number of internal nodes in such a trie is known to be
[
\frac{N}{\ln 2} + o(N) \approx 1.44,N,
]
as derived in the analysis leading to equation (30) of the text. Because (1/\ln 2 > 1), this average exceeds (N) for all sufficiently large (N). Consequently, radix exchange often requires more partitioning stages than the maximum possible for quicksort.
The fundamental reason is that radix exchange uses a fixed, data‑independent sequence of bit tests. A single key can participate in many partitioning stages - one for each bit of its distinguishing prefix - whereas in quicksort every key becomes a pivot at most once, so each stage finalizes at least one key and the total number of stages is bounded by (N).
This completes the proof. ∎