logo

Radix Sort 📂Algorithm

Radix Sort

Algorithm

Assuming we have data comprised of numbers with digits limited to kk. In this case, the data will be sorted according to the following algorithm, and its time complexity is O(n)O (n).

Sort by comparing the i=1,,ki = 1 , \cdots , kth 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

19081297721413 190 \\ 812 \\ 9 \\ 77 \\ 21 \\ 413 For instance, a dataset of n=6n=6 numbers limited to digits of k=3k=3 can be sorted in 36=183 \cdot 6 = 18 passes. By first sorting by the i=1i=1th digit, 190021812413077009 19 {\color{red}0} \\ 02 {\color{red}1} \\ 81 {\color{red}2} \\ 41 {\color{red}3} \\ 07 {\color{red}7} \\ 00 {\color{red}9} then by the i=2i=2th digit, 009812413021077190 0 {\color{red}0} 9 \\ 8 {\color{red}1} 2 \\ 4 {\color{red}1} 3 \\ 0 {\color{red}2} 1 \\ 0 {\color{red}7} 7 \\ 1 {\color{red}9} 0 and finally by the i=ki=kth digit, 009021077190413812 \color{red}{0} 0 9 \\ {\color{red}0} 21 \\ {\color{red}0} 77 \\ {\color{red}1} 90 \\ {\color{red}4} 13 \\ {\color{red}8} 12 it is important to note that since kk is given as a constant number of digits, the result is trivially kn=O(n)kn = O(n). However, if the digit count exceeds the number of data elements, the complexity becomes O(n2)O(n^2). 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 00 to 99, you simply gather and then merge numbers according to each digit. If it’s 00, gather all 00s together, if it’s 11, gather all 11s together, …, if it’s 99, gather all 99s together into an array and then merge them in order, thus completing the sort for that digit. Repeat this for digits up to kk, resulting in a total operation count of kn=O(n)k n = O(n).

Code

Here’s an implementation of Radix Sort in Julia.