Design and Analysis of Algorithms
Sorting in linear time
Reference:
CLRS Chapter 8
Topics:
• Lower bound for sorting
• Counting sort
• Radix sort
• Bucket sort
Huo Hongwei 1
How fast can we sort?
• All the sorting algorithms we have seen so far are
comparison sorts: only use comparisons to determine
the relative order of elements.
– E.g., insertion sort, merge sort, quicksort, heapsort.
– The best worst-case running time that we’ve seen for
comparison sorting is O(n lg n).
• Q: Is O(n lg n) the best we can do?
• A : Yes, as long as we use comparison sorts
• TODAY: Prove any comparison sort has Ω(n lg n) worst
case running time
Huo Hongwei 2
Decision-tree example
• Sort 1:2
1 2 n
2:3 1:3
123 1:3 213 2:3
132 312 231 321
• Each internal node is labelled i:j for i,j ∈ {1, 2,…, n}.
– The left subtree shows subsequent comparisons if a ≤
i
a .
j
– The right subtree shows subsequent comparisons if ai
≥ a .
j
Huo Hongwei 3
Decision-tree example
• Sort = <9, 4, 6> 1:2 9 ≥ 4
1 2 3
2:3 1:3
123 1:3