Each internal node is labelled i : j for i, j ∈ {1, 2, . . . , n} Each internal node is labelled i : j for i, j ∈ {1, 2, . . . , n}
I The left subtree shows subsequent comparisons if ai ≤ aj I The left subtree shows subsequent comparisons if ai ≤ aj
I The right subtree shows subsequent comparisons if ai > aj I The right subtree shows subsequent comparisons if ai > aj
Each internal node is labelled i : j for i, j ∈ {1, 2, . . . , n} Each internal node is labelled i : j for i, j ∈ {1, 2, . . . , n}
I The left subtree shows subsequent comparisons if ai ≤ aj I The left subtree shows subsequent comparisons if ai ≤ aj
I The right subtree shows subsequent comparisons if ai > aj I The right subtree shows subsequent comparisons if ai > aj
Theorem
Any decision tree that can sort n elements must have height Ω(n log n).
Proof. Corollary
The tree must contain at least n! leaves as there are n! different
Merge sort and heap sort are asymptotically optimal comparison sorting
permutations to choose from. A binary tree of height h has ≤ 2h leaves.
algorithms.
Therefore n! ≤ number of leaves ≤ 2h .
n n
h ≥ log n! ≥ log( )
2 2
h ∈ Ω(n log n)
If k ∈ Θ(n) then the total running time of counting sort is Θ(n). A crucial property of counting sort is that it is stable.
I But we proved an Ω(n log n) lower bound! What happened? I Why did we bother with the last two loops of counting sort?
Answer: I We want the sort to be stable.
I We proved a lower bound for comparison sorts I It preserves the order of equal elements
I Counting sort is not a comparison sort What other sorting algorithms are stable?
I In fact there is not a single comparison in it! See blackboard...
40000
30000
I Therefore
T (n, b) ∈ Θ(nb/ log n)
20000
I For numbers in the range from 0 to nd −1 , we have b = d log n ⇒ radix
10000 sort runs in Θ(dn) time.
0
2 4 6 8 10 12 14
r
Summary
I Counting sort runs in Θ(n) time when the values to be sorted are less
than n
I Radix is fast for 32-bit numbers, for example, where only 4 passes of
count sort and an auxiliary array of size 28 = 256 are needed if we set
r =8
I Merge sort or quicksort will need at least 11 linear time passes if there
are, say, ≥ 2000 values to be sorted
I However, radix sort has poor memory locality and so a well tuned
quicksort may be faster in practice