|
| 1 | +# Radix Sort |
| 2 | + |
| 3 | +Radix Sort is a non-comparison based, stable sorting algorithm. |
| 4 | +We first treat each element as a string with *w* digits (Padding elements that have |
| 5 | +less than *w* digits). |
| 6 | + |
| 7 | +From the least-significant digit to the most-significant digit, we constantly |
| 8 | +split them into ten queues corresponding to the number range *[0, 9]*. We then move |
| 9 | +through the queue and concatenate the elements back into a list at the next iteration. |
| 10 | + |
| 11 | +This takes advantage of the concept of place value. |
| 12 | +(The value of a digit in a number relative to its position within the number) |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +*Source: Level Up Coding* |
| 17 | + |
| 18 | +## Complexity Analysis |
| 19 | +**Time**: |
| 20 | +Note that we will always need to iterate through 10 different queues to rebuild our original array, |
| 21 | +and we iterate through all *w* positions, this results in: |
| 22 | + |
| 23 | +- Worst case: O(w * (n + 10)) |
| 24 | +- Average case: O(w * (n + 10)) |
| 25 | +- Best case (sorted array): O(w * (n + 10)) |
| 26 | + |
| 27 | +**Space**: O(n + k) |
| 28 | + |
| 29 | +## Notes |
| 30 | +- Radix sort's time complexity is dependent on the maximum number of digits in each element, |
| 31 | +hence it is ideal to use it on integers with a large range and with little digits. |
| 32 | +- This could mean that Radix Sort might end up performing worst on small sets of data |
| 33 | +if any one given element has a in-proportionate amount of digits. |
| 34 | +- Counting sort is used as a sub-routine within the Radix Sort process. |
| 35 | + |
| 36 | +### Common Misconception |
| 37 | +- While not immediately obvious, we can see that radix sort is a stable sorting algorithm as |
| 38 | + they are enqueued in a manner where the first observed element will be at the head of the queue. |
| 39 | +- While it is non-comparison based, not that total ordering of elements is still required - |
| 40 | + except now this property is forced upon the algorithm in the manner of the queues. |
0 commit comments