Radix Sort - Amritam Sarcar

advertisement
Tutorial on Non-comparison sort : Radix Sort
The popular sorts like the merge sort, quick sort, bubble sort, heap
sorts and others are sorting algorithms which sort their array using
comparisons.
The question that we are trying to ask is: Can we sort without
comparing?
The obvious answer is yes. But then, how? Obviously, non-comparing
sorts can be sorted in linear time. This is our hypothesis. Of the many
such non-comparison sorts there are counting sort, radix sort and
bucket sort. In order to exemplify the working of the radix sort,
counting sort is used.
History:
How did IBM made its money? There were Punch card readers for
census tabulation in early 1900’s. The obvious way out would be to
sorts on one column at a time. Infact it is this algorithm that is the
backbone of the radix sort. Actually, it is the machine that now
extends this human technique to sort multi-column.
Key idea: Sort the least significant digits first.
To sort d digits:
RADIX-SORT(A, d)
for i ← 1 to d
do use a stable sort to sort array A on digit I sorted
Correctness:
• Induction on number of passes (i in pseudocode).
• We Assume digits 1, 2, . . . , i − 1 are sorted.
• We Show that a stable sort on digit i leaves digits 1, . . . , i sorted:
• If 2 digits in position i are different, ordering by position i is correct,
and positions 1, . . . , i − 1 are irrelevant.
• If 2 digits in position i are equal, numbers are already in the right
order (by inductive hypothesis). The stable sort on digit i leaves them
in the right order.
Analysis:
Assume that we use counting sort as the intermediate sort.
• O (n + k) per pass (digits in range 0, . . . , k)
• d passes
• O (d(n + k)) total
• If k = O(n), time = O (dn).
Now, How do we break each key into digits?
• n words.
• b bits/word.
• Breaking into r -bit digits. We have d = b/r.
• Since we Use counting sort, k = 2r − 1.
Thus we have for radix sort, T(n) = O(b/r (n + 2r – 1))
Example:
For 64-bit words, 8-bit digits. b = 64, r = 8, d = 64/8 = 8, k =
16− 1 = 15.
Since radix sort, sorts in linear time it is one of the best sorting
algorithm.
Example:
Sample input is: 123,432,655, 433,565,743.
First pass:
Sorting the least significant bit. We have
432, 123, 433, 743, 565.
Second pass:
Sorting the next LSB;
123, 432, 433, 743, 565.
Third pass:
Sorting the MSB;
123, 432, 433, 565, 743.
Download