Radix sort does not compare numbers, instead, it takes advantage of number’s property, e.g. length, to fast sort numbers without having to do comparison.
- Given a list of numbers
- Find the maximum length among all numbers, that is, the largest number’s length
- Start a loop from 0 up to the maximum length
- i means the i-th position counting from the right
- In each iteration, we sort the numbers by the digit at the specified i position.
For example, we want to sort this array of numbers:
The largest number = 9763, the maximum length = 4, so we will have 4 iterations.
Iteration 0: Sort the array by the digit at index 0,
Note that index is counting from right
Iteration 1: Sort the array by the digit at index 1
Iteration 2: Sort the array by the digit at index 2
Iteration 3: Sort the array by the digit at index 3