We have met sorts that take n log n steps. Can any algorithm that sorts by comparing elements do better? The answer is no.

The Decision Tree Model

Any comparison sort can be drawn as a decision tree. Each internal node is a comparison between two elements, say a[i] ≤ a[j]?, with two outgoing branches for yes and no; each leaf is one outcome, a particular ordering of the input. The running time on an input is the number of comparisons on the path from the root to its leaf, so the worst case is the height of the tree.

Counting Leaves

There are n! different orderings (permutations) of n distinct elements, and a correct algorithm must reach a different leaf for each, so the tree has at least n! leaves. A binary tree of height h has at most 2^h leaves. Hence 2^h ≥ n!, that is h ≥ log₂(n!). Using Stirling's approximation, log₂(n!) = n log₂ n − 1.44n + O(log n), which is Ω(n log n). A simpler argument: n! ≥ (n/2)^(n/2), so log₂ n! ≥ (n/2) log₂(n/2), which is Ω(n log n).

Consequences

Every comparison-based sorting algorithm needs Ω(n log n) comparisons in the worst case. Therefore merge sort and heap sort, which take O(n log n), are asymptotically optimal. Quick sort is optimal on average. To beat n log n an algorithm must use more than comparisons: counting sort, radix sort and bucket sort use the values of the keys themselves and can run in linear time when keys are integers in a small range. The bound also gives the minimum number of comparisons for small n: for 4 elements at least ⌈log₂ 24⌉ = 5 comparisons are required.

Definition: Decision tree. A binary tree model of a comparison algorithm in which internal nodes are comparisons and leaves are possible outputs; its height is the worst-case number of comparisons.

Table 2.5: Minimum comparisons from log₂(n!)

n n! (leaves needed) ⌈log₂ n!⌉
3 6 3
4 24 5
5 120 7
10 3,628,800 22

Example 2.5: Lower bound for three elements

Problem: Find the minimum number of comparisons needed in the worst case to sort three distinct elements.

Solution:

  1. There are 3! = 6 possible orderings, so the decision tree needs at least 6 leaves.
  2. A tree of height h has at most 2^h leaves; 2² = 4 < 6, but 2³ = 8 ≥ 6.
  3. So h ≥ 3, i.e. ⌈log₂ 6⌉ = 3.
  4. Three comparisons are also enough (insertion sort on 3 elements needs at most 3).

Result: Exactly 3 comparisons are needed in the worst case.

import math
for n in (3, 4, 5, 10, 100):
    f = math.factorial(n)
    print(f"n={n:<4} ceil(log2 n!) = {math.ceil(math.log2(f)):<4}"
          f" n*log2(n) = {n * math.log2(n):.1f}")

Output

n=3    ceil(log2 n!) = 3    n*log2(n) = 4.8
n=4    ceil(log2 n!) = 5    n*log2(n) = 8.0
n=5    ceil(log2 n!) = 7    n*log2(n) = 11.6
n=10   ceil(log2 n!) = 22   n*log2(n) = 33.2
n=100  ceil(log2 n!) = 525  n*log2(n) = 664.4

Common mistakes

  • Claiming no sort can beat n log n; only comparison-based sorts cannot.
  • Forgetting that the tree needs n! leaves, not n.
  • Mixing the height of the tree (worst case) with the average depth of leaves.
  • Thinking the bound makes quick sort's worst case n² optimal; it is not.

Key takeaways

  • A comparison sort corresponds to a decision tree with at least n! leaves.
  • Height h satisfies 2^h ≥ n!, so h ≥ log₂ n! = Ω(n log n).
  • Merge sort and heap sort are asymptotically optimal comparison sorts.
  • Counting, radix and bucket sort avoid comparisons and can be linear.

Practice questions

  1. How many leaves must a decision tree for sorting n elements have?
  2. What is the lower bound on comparison-based sorting?
  3. Name a sort that is not comparison-based.

Answers to the practice questions

  1. At least n!, one for every ordering.
  2. Ω(n log n) comparisons in the worst case.
  3. Counting sort (also radix sort and bucket sort).

Sources

  • Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit II