Radixsort
Radixsort
Fall 2006
There exists an input ordering that corresponds to each root-to-leaf path to arrive at a
sorted order. For decision tree of insertion sort, the longest path is O(N2).
Sorting IV / Slide 7
Counting Sort
☛ Assume N integers are to be sorted, each is in the range 1 to M.
☛ Define an array B[1..M], initialize all to 0 ⇒ O(M)
☛ Scan through the input list A[i], insert A[i] into B[A[i]] ⇒ O(N)
☛ Scan B once, read out the nonzero integers ⇒ O(M)
Total time: O(M + N)
■ if M is O(N), then total time is O(N)
■ Can be bad if range is very big, e.g. M=O(N2)
N=7, M = 9,
Want to sort 8 1 9 5 2 6 3
1 2 3 5 6 8 9
Output: 1 2 3 5 6 8 9
Sorting IV / Slide 11
Counting sort
☛ What if we have duplicates?
☛ B is an array of pointers.
☛ Each position in the array has 2 pointers:
head and tail. Tail points to the end of a linked
list, and head points to the beginning.
☛ A[j] is inserted at the end of the list B[A[j]]
☛ Again, Array B is sequentially traversed and
each nonempty list is printed out.
☛ Time: O(M + N)
Sorting IV / Slide 12
Counting sort
M = 9,
Wish to sort 8 5 1 5 9 5 6 2 7
1 2 5 6 7 8 9
Output: 1 2 5 5 5 6 7 8 9
Sorting IV / Slide 13
Radix Sort
☛ Extra information: every integer can be
represented by at most k digits
■ d1d2…dk where di are digits in base r
■ d1: most significant digit
■ dk: least significant digit
Sorting IV / Slide 14
Radix Sort
☛ Algorithm
■ sort by the least significant digit first (counting sort)
=> Numbers with the same digit go to same bin
■ reorder all the numbers: the numbers in bin 0
precede the numbers in bin 1, which precede the
numbers in bin 2, and so on
■ sort by the next least significant digit
■ continue this process until the numbers have been
sorted on all k digits
Sorting IV / Slide 15
Radix Sort
☛ Least-significant-digit-first
Radix Sort
☛ Does it work?
Radix Sort
Example 2: sorting cards
■ 2 digits for each card: d1d2
■ d1 = ♠♥♣♦: base 4
♦≤♣≤♥≤♠
■ d2 = A, 2, 3, ...J, Q, K: base 13
A ≤ 2 ≤ 3 ≤ ... ≤ J ≤ Q ≤ K
■ ♦2 ≤ ♣2 ≤ ♥5 ≤ ♠K
Sorting IV / Slide 19
A=input array, n=|numbers to be sorted|,
d=# of digits, k=the digit being sorted, j=array index
// base 10
// FIFO
Radix Sort
☛ Increasing the base r decreases the number of
passes
☛ Running time
■ k passes over the numbers (i.e. k counting sorts, with range
being 0..r)
■ each pass takes 2N
■ total: O(2Nk)=O(Nk)
■ r and k are constants: O(N)
☛ Note:
■ radix sort is not based on comparisons; the values are used
as array indices
■ If all N input values are distinct, then k = Ω(log N) (e.g., in
binary digits, to represent 8 different numbers, we need at
least 3 digits). Thus the running time of Radix Sort also
become Ω(N log N).