CS 332: Algorithms: Linear-Time Sorting Algorithms
CS 332: Algorithms: Linear-Time Sorting Algorithms
Sorting So Far
Insertion sort:
Easy to code
Fast on small inputs (less than ~50 elements)
Fast on nearly-sorted inputs
O(n2) worst case
O(n2) average (equally-likely inputs) case
O(n2) reverse-sorted case
Sorting So Far
Merge sort:
Divide-and-conquer:
Split array in half
Recursively sort subarrays
Linear-time merge step
Sorting So Far
Heap sort:
Sorting So Far
Quick sort:
Divide-and-conquer:
Partition array into two subarrays, recursively sort
All of first subarray < all of second subarray
No merge step needed!
Decision Trees
Decision Trees
n
n!
e
n
Thus:
h lg
e
So we have
n
h lg
e
n lg n n lg e
n minimum
the
lg n
Thus
height of a decision tree is (n lg n)
Counting sort
The algorithm:
Input: A[1..n], where A[j] {1, 2, 3, , k}
Output: B[1..n], sorted (notice: not sorting in place)
Also: Array C[1..k] for auxiliary storage
Counting Sort
1
2
3
4
5
6
7
8
9
10
CountingSort(A, B, k)
for i=1 to k
C[i]= 0;
for j=1 to n
C[A[j]] += 1;
for i=2 to k
C[i] = C[i] + C[i-1];
for j=n downto 1
B[C[A[j]]] = A[j];
C[A[j]] -= 1;
Counting Sort
1
2
3
4
5
6
7
8
9
10
CountingSort(A, B, k)
for i=1 to k
Takes time O(k)
C[i]= 0;
for j=1 to n
C[A[j]] += 1;
for i=2 to k
C[i] = C[i] + C[i-1];
Takes time O(n)
for j=n downto 1
B[C[A[j]]] = A[j];
C[A[j]] -= 1;
Counting Sort
Usually, k = O(n)
Thus counting sort runs in O(n) time
Counting Sort
Cool! Why dont we always use counting sort?
Because it depends on range k of elements
Could we use counting sort to sort 32 bit
integers? Why or why not?
Answer: no, k too large (232 = 4,294,967,296)
Counting Sort
How did IBM get rich originally?
Answer: punched card readers for census
tabulation in early 1900s.
Radix Sort
Intuitively, you might sort on the most significant
digit, then the second msd, etc.
Problem: lots of intermediate piles of cards (read:
scratch arrays) to keep track of
Key idea: sort the least significant digit first
RadixSort(A, d)
for i=1 to d
StableSort(A) on digit i
Example: Fig 9.3
Radix Sort
Can we prove it will work?
Sketch of an inductive argument (induction on the
number of passes):
Radix Sort
What sort will we use to sort on digits?
Counting sort is obvious choice:
Radix Sort
Radix Sort
Fast
Asymptotically fast (i.e., O(n))
Simple to code
A good choice
The End