Intuition

Consider a list with elements all given in a specific base. For example, consider the list with integers in base , with at most 3 digits.

Because all of the elements are in the same base, we could theoretically sort by sorting them by their digits, in order of least-significant to most-significant digit!

If we were to use a stable sorting algorithm to do this, we can ensure that the relative order of elements with some identical digits will be preserved!

We can do this with . Starting from the least significant (rightmost) digit, and working our way to the most significant (leftmost) digit, we sort our array as follows:

Radix Sort

Algorithm

In Radix Sort, and given an array with elements in the same base with at most digits, we can sort it by applying a stable sorting algorithm to sort elements by each individual digit, in order of least (rightmost) to most (leftmost) significant digit.

Typically, Radix Sort uses Counting Sort as the stable sorting algorithm, because we have a predefined base we can sort on!

Time Complexity

Applying Counting Sort as the stable sorting algorithm, we see that for each sort with regards to a digit, we get time , where is our base (as the digits can only range from ).

Applying this for every digit up to digits, we get final time complexity

Now, assuming is fixed, and doesn’t depend on , we have time complexity ! However, this assumes independence of and , and may not hold in all cases. Consider the following example.

Example: Time Complexity of Radix Sort

Let’s sort a list of nonzero ints between 0 and , each represented in binary. What’s the time complexity?

It takes binary digits to represent our elements in the list, giving us time complexity .

Furthermore, we have auxiliary space , as we will take this much space at any time due to Counting Sort.