Two attributes of the key are of importance to the Radix Sort. First, the is the key length, or the number of positions in the key. A string is composed of discrete character positions, so the key length is simply the string length. An integer is considered as composed of discrete digit positions, so the key length is the number of digits in the key. The second attribute is the radix, which is the number of possible values that can be assigned to a single position in the key.
For example, if the key is a decimal integer, each position is a digit and has ten possibilities: 0..9. If the key is a string of letters and case is not important, then each position has 26 possibilities: 'a'..'z'.
In a nutshell, radix sort divides the items to be sorted into as many subgroups as there are possible values for each position in the key (radix subgroups). Then the subgroups are combined back into a single list and the process is repeated. Beginning with the least-significant position in the key, regroup the values in order, and repeat the process as many times as there are positions in the key, moving one position to the left each time.
813 516 223 462 723 561 416 834 616 323Since the key length is three, Radix Sort is going to make 3 passes through this list. One pass for each of the 3 digits.
On the first pass through the list, the third (or least significant) digit is selected for sorting.
Taking keys one at a time from the list, they are distributed into radix subgroups, or "bins."
bin0 bin1 561 bin2 462 bin3 813 223 723 323 bin4 834 bin5 bin6 516 416 616 bin7 bin8 bin9Next the items are taken out of the bins in orderfrom 0 to 9 and recombined into a single list.
561 462 813 223 723 323 834 516 416 616On the second pass, the middle digit is selected, and the keys are distributed into bins based on the value of the middle digit.
bin0 bin1 813 516 416 616 bin2 223 723 323 bin3 834 bin4 bin5 bin6 561 462 bin7 bin8 bin9The items are then combined back into a single list:
813 516 416 616 223 723 323 834 561 462On the third and final pass, the first digit (most significant) is selected for sorting, and the keys are distributed based on the value of their first digit.
bin0 bin1 bin2 223 bin3 323 bin4 416 462 bin5 516 561 bin6 616 bin7 723 bin8 813 834 bin9After reassembling the bins into the final list, we see the items are sorted.
223 323 416 462 516 561 616 723 813 834
"Reusable" means that it is a stand-alone procedure that can be invoked from a separate module.
Create a test driver that reads a list of character strings, one per line, from standard input. The command line should contain the key length and the radix lower and upper bounds, in that order.
For example (in Java):java RadixDriver 3 0 9The driver should ignore lines that do not contain the number of characters specified in key length. The driver should ignore lines that contain characters outside the radix range. Have the data sorted by the Radix Sort module, then output the results, one per line, to standard output.
Note: Please design your own solution. While this algorithm is easy to find on the Web or in textbooks, please write your own algorithm from scratch.