Radix Sort
Algorithm
Assuming we have data comprised of numbers with digits limited to . In this case, the data will be sorted according to the following algorithm, and its time complexity is .
Sort by comparing the th digits of each number.
Explanation
Radix Sort is a clever method that can sort data in linear time without comparing the data elements, overcoming the limits of comparison sorting algorithms since it’s limited to datasets without floating-point numbers due to digit limitation.
Example
For instance, a dataset of numbers limited to digits of can be sorted in passes. By first sorting by the th digit, then by the th digit, and finally by the th digit, it is important to note that since is given as a constant number of digits, the result is trivially . However, if the digit count exceeds the number of data elements, the complexity becomes . In such cases, it is better to use a comparison sorting algorithm.
Proof
Strategy: If you’ve understood the process above, there’s literally no need to see a proof.
To sort numbers from to , you simply gather and then merge numbers according to each digit. If it’s , gather all s together, if it’s , gather all s together, …, if it’s , gather all s together into an array and then merge them in order, thus completing the sort for that digit. Repeat this for digits up to , resulting in a total operation count of .
■
Code
Here’s an implementation of Radix Sort in Julia.