After this page, you should be able to:
Connect decision-tree leaves to a worst-case comparison lower bound.
Explain why restricted keys and favourable inputs need separate reasoning.
Builds on: Sorting comparisons and asymptotic bounds.
Specify the model first¶
A comparison sort discovers key order by comparisons between keys. We consider arbitrary inputs of distinct keys, so a binary comparison has two possible outcomes. There are possible relative input orders, and a correct algorithm must distinguish all of them.
This is a statement about information gained from comparisons. It is not a claim that every sorting problem, under every possible key restriction, needs the same amount of time.
A decision tree records possible histories¶
Each internal node represents a comparison; its outgoing edges represent outcomes. A root-to-leaf path records the comparisons for one input. A leaf specifies the resulting order of the input records. For three distinct records A, B and C, a valid tree has six leaves:
Here ABC means A has the smallest key, followed by B and C; the letters name records, not fixed key values. Some paths need two comparisons and others three. The height measures the worst-case number of comparisons, not the number on every input.
Count leaves, then bound height¶
A binary tree of height has at most leaves. Distinguishing all orders therefore requires
To turn this into a lower bound in terms of , use the large factors of the factorial. For , at least factors are at least , so
Also , giving . Together these show . The lower inequality is essential: an upper bound on the factorial alone cannot establish a lower bound on sorting.
Thus every comparison sort has an input requiring comparisons. Merge sort achieves worst-case comparisons, so its comparison growth is asymptotically optimal in this model. This does not make it best for every input size, memory limit or input order. Insertion sort can still take linear time on sorted input because the lower bound concerns the worst case over all orders.
Why restricted-key sorts can go further¶
Suppose keys are integers in 0..k-1. Counting sort can use a key as an array index, count occurrences, and reconstruct the ordered result in time. Its information access is not limited to comparisons between pairs of keys, so the decision-tree bound above does not apply.
That speed comes with assumptions and storage costs. A count array needs entries; a common stable record-sorting implementation also uses output space. If is huge relative to , counting may be unattractive. Numeric-looking data alone does not guarantee a useful small key range.
Check your understanding¶
Predict the answer before opening the explanation.
1. Why does returning an ordered empty list fail to avoid the lower bound?
Show explanation
It fails the sorting contract: the input records must be preserved.
2. Does linear time on one sorted input contradict the worst-case lower bound?
Show explanation
No. A particular favourable input can be easy while some other inputs require Ω(n log n) comparisons.
Continue to Sequences, stacks, queues and deques, return to the sorting overview, or find a term in the concept index.