Professional Documents
Culture Documents
Radixsort
Radixsort
Radixsort
Fall 2005
Sorting IV / Slide 2
and heapsort
Are
Sorting IV / Slide 3
Sorting IV / Slide 4
Sorting IV / Slide 5
Sorting IV / Slide 6
Sorting IV / Slide 7
Sorting IV / Slide 8
Any
Sorting IV / Slide 9
Counting
Sorting IV / Slide 10
Counting Sort
Scan through the input list A[i], insert A[i] into B[A[i]] O(N)
N=7, M = 9,
Want to sort 8 1 9
1 2 3
5 2 6 3
5
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
6 7
5
5
Output: 1 2 5 5 5 6 7 8 9
Sorting IV / Slide 13
Radix Sort
Extra
Sorting IV / Slide 14
Radix Sort
Algorithm
Sorting IV / Slide 15
Radix Sort
Least-significant-digit-first
Sorting IV / Slide 16
Sorting IV / Slide 17
Radix Sort
Does
it work?
Clearly,
If
Sorting IV / Slide 18
Radix Sort
Example 2: sorting cards
d2 = A, 2, 3, ...J, Q, K: base 13
A
2 3 ... J Q K
2 2 5 K
Sorting IV / Slide 19
// base 10
// FIFO
// d times of counting sort
Sorting IV / Slide 20
Radix Sort
Increasing
of passes
Running time