LOGBOOK

HELP

Quiz Entry - updated: 2026.11.27

Why must the decision tree of a correct comparison sort have at least $n!$ leaves?

Because $n$ distinct keys can arrive in $n!$ different orders, and each order needs a different rearrangement to come out sorted. If two different input orders ended in the same leaf, the algorithm would apply the same rearrangement to both, and at least one of them would come out wrong.

A leaf is where a run ends: the algorithm stops comparing and outputs the elements in one fixed arrangement, such as "first the third input element, then the first, then the second". Take two inputs that differ only in the order of two values at the same positions, $\ldots 4 \ldots 5 \ldots$ and $\ldots 5 \ldots 4 \ldots$. To sort the first, the 4 has to end up in front of the 5; applied to the second input, the very same arrangement puts the 5 in front of the 4. One fixed arrangement cannot be right for both, so the two inputs must reach different leaves.

The same argument works for any two of the $n!$ orders, so the tree needs at least one leaf per order: $3! = 6$ leaves for three elements, $10! = 3\,628\,800$ for ten. A tree may have more leaves than that (an algorithm can ask a pointless question whose second branch no input ever takes), but never fewer.

From Quiz: ADS / Sorting Lower Bound, Bucket-Sort and Radix-Sort | Updated: Nov 27, 2026