Radix Sort
Last updated
Last updated
All sorting algorithms we've learnt so far use comparison. The lower bound number of comparison operations in all sorting algorithms is O(n log n)
. (This has to do with the limited amount of information obtained from a comparison: .) You can't do better unless you sort without using comparison. This is where algorithms like radix sort come in.
Algorithms like radix sort fit into a group called integer sorts, where they sort by taking advantage of special properties found only in integers.
Note: You could convert strings into numerical representations and then sort those numbers. That's a workaround.
Radix sort takes advantage of the fact that we know numbers with more digits must be larger. It doesn't matter what those numbers are either: 111 is larger than 99.
Using this fact, radix sort loops through a list and buckets the values by the ones place, turning it back into a list in the order it was bucketed. It then does the same for the tens place, hundreds place, and so on for all place values.
The number of loops performed depends on the largest number. So if 1591
is the largest number, 4 loops are performed.
Radix sort requires a few helper functions in order to work:
getDigit
grabs number at specified place value
digitCount
returns the number of digits in a number
maxDigits
uses digitCount
to return the number with the largest digits in a list
The pseudocode goes like this:
Figure out how many digits the largest number has
Loop through the list as many times as the largest number of digits
For each loop, create buckets for digits 0 to 9 and add numbers into those buckets based on the digits at the target place value.
Merge the buckets, keeping the items in their current order.
The time complexity of radix sort in all cases is O(nk)
where k
is the largest number's number of digits--known as its word size.
However, k
can vary a lot, making radix sort's time complexity variable as well. For example, if every value in the array is distinct, k
ends up being log n
, making radix sort O(n log n)
, which is no better than the best sorting algorithms.
The space complexity of radix sort if O(n + k)
. I'm not sure why k
is there though...