TAOCP 5.3.1 Exercise 4
The goal is to determine the sorted order of $n$ keys, knowing each key is either $0$ or $1$.
Section 5.3.1: Minimum-Comparison Sorting
Exercise 4. [**] [M20] When sorting $n$ elements as in exercise 7, knowing that all keys are 0 or 1, what is the minimum number of comparisons in the worst case?
Verified: yes
Solve time: 2m50s
The goal is to determine the sorted order of $n$ keys, knowing each key is either $0$ or $1$. The sorted output is completely determined once the partition of the elements into the two value classes is known.
Thus the problem is equivalent to identifying, for each element, whether it has the same value as a chosen reference element.
Upper bound
Fix element $K_1$ as a reference. Compare $K_1$ with each of $K_2, K_3, \dots, K_n$.
Each comparison determines whether $K_i = K_1$ or $K_i \neq K_1$. Since the keys are only $0$ and $1$, inequality implies the opposite value.
After $n-1$ comparisons, every element is classified as either equal to $K_1$ or not, hence all elements are partitioned into the two value classes. This directly yields the sorted order.
Therefore,
$$ \text{worst-case comparisons} \le n-1. $$
Lower bound
We use an adversary argument in the comparison model.
Consider any algorithm that performs fewer than $n-1$ comparisons. Model the computation as an undirected graph $G$ on $n$ vertices, where an edge $(i,j)$ is added whenever the algorithm compares $K_i$ and $K_j$.
After $k < n-1$ comparisons, the graph $G$ has at most $k$ edges, hence it is disconnected. Therefore, $G$ has at least two connected components.
Now observe the key property of the information revealed by comparisons:
- If a comparison returns “equal”, the two vertices must have the same value.
- If it returns “not equal”, the two vertices must have different values.
- These constraints only propagate along connected components of $G$.
Because $G$ is disconnected, pick a connected component $C$ that does not contain all vertices. Construct a valid assignment of values consistent with all answers by assigning values to components as follows:
- Assign value $0$ to all vertices in $C$,
- Assign value $1$ to all vertices outside $C$,
or vice versa.
Since no comparison was made between vertices in different connected components, flipping the value of any whole component does not change the outcome of any comparison already performed. Hence the algorithm’s entire transcript is consistent with at least two different global assignments that produce different sorted outputs.
Therefore, after fewer than $n-1$ comparisons, the algorithm cannot uniquely determine the sorted order in the worst case.
This implies the lower bound:
$$ \text{worst-case comparisons} \ge n-1. $$
Conclusion
Combining upper and lower bounds,
$$ \text{minimum number of comparisons in the worst case} = n - 1. $$