A non-comparative sorting algorithm. It avoids comparison by creating and distributing elements into buckets according to their radix. For elements with more than one significant digit, this bucketing process is repeated for each digit, while preserving the ordering of the prior step, until all digits have been considered.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
{
"arr": "[170,45,75,90,802,24,2,66]",
"max": 802
}Overview
Radix Sort sorts integers one digit at a time, starting from the least significant digit (LSD). Each pass is a stable Counting Sort on a single digit, so after the pass for the tens digit, numbers with equal tens digits are still ordered by their units digit from the previous pass.
The number of passes equals the number of digits in the largest value. The loop variable exp is 1, 10, 100 and so on, and Math.floor(arr[i] / exp) % 10 extracts the current digit.
With the default input 170, 45, 75, 90, 802, 24, 2, 66, the maximum is 802, so there are three passes. After the units pass the order is 170, 90, 802, 2, 24, 45, 75, 66; after the tens pass it is 802, 2, 24, 45, 66, 170, 75, 90; the hundreds pass finishes the job.
Step by step
- Find the maximum value to know how many digits to process.
- For
exp = 1, 10, 100, ...whilemax / exp >= 1: - Count how many numbers have each digit 0–9 at position
exp. - Turn the counts into prefix sums and place numbers into
outputfrom right to left (for stability). - Copy
outputback and move to the next digit.
Complexity
| Case | Cost | Why |
|---|---|---|
| Best case | O(d · (n + b)) | d digits, n elements, base b = 10 buckets per pass. |
| Average case | O(d · (n + b)) | Input order does not matter. |
| Worst case | O(d · (n + b)) | Effectively linear when d is small and fixed. |
| Extra space | O(n + b) | Output array plus the 10-slot count array. |
When to use it
- Large arrays of fixed-width integers, IDs, IP addresses or dates.
- Strings of equal length (sort character by character from the right).
- When the key length d is small compared with log n.
Common mistakes
- The implementation shown assumes non-negative integers; negatives need to be offset or sorted separately.
- Using an unstable per-digit sort breaks the algorithm — later passes would scramble earlier ones.
- A single very large value increases d, and with it the number of passes, for the whole array.
A dry run is the best way to check your understanding of Radix Sort: follow each row, predict the next one, and compare. The run below takes 233 steps in total. Steps that only re-print loop counters are left out so the decisions stand out. The Line column refers to the line numbers in the code listing above.
- Input
- 170, 45, 75, 90, 802, 24, 2, 66
- Result
- return value = [2, 24, 45, 66, 75, 90, 170, 802]
| Step | Line | Array | What happens |
|---|---|---|---|
| 1 | 2 | [170, 45, 75, 90, 802, 24, 2, 66] | max = 802 |
| 63 | 23 | [170, 90, 802, 2, 24, 45, 75, 66] | i = 0 |
| 64 | 22 | [170, 45, 75, 90, 802, 24, 2, 66] | i = 1 |
| 65 | 23 | [170, 90, 802, 2, 24, 45, 75, 66] | i = 1 |
| 66 | 22 | [170, 90, 75, 90, 802, 24, 2, 66] | i = 2 |
| 67 | 23 | [170, 90, 802, 2, 24, 45, 75, 66] | i = 2 |
| 68 | 22 | [170, 90, 802, 90, 802, 24, 2, 66] | i = 3 |
| 69 | 23 | [170, 90, 802, 2, 24, 45, 75, 66] | i = 3 |
| 70 | 22 | [170, 90, 802, 2, 802, 24, 2, 66] | i = 4 |
| 71 | 23 | [170, 90, 802, 2, 24, 45, 75, 66] | i = 4 |
| 72 | 22 | [170, 90, 802, 2, 24, 24, 2, 66] | i = 5 |
| 73 | 23 | [170, 90, 802, 2, 24, 45, 75, 66] | i = 5 |
| 74 | 22 | [170, 90, 802, 2, 24, 45, 2, 66] | i = 6 |
| 75 | 23 | [170, 90, 802, 2, 24, 45, 75, 66] | i = 6 |
| 140 | 23 | [802, 2, 24, 45, 66, 170, 75, 90] | i = 0 |
| 141 | 22 | [802, 90, 802, 2, 24, 45, 75, 66] | i = 1 |
| 142 | 23 | [802, 2, 24, 45, 66, 170, 75, 90] | i = 1 |
| 143 | 22 | [802, 2, 802, 2, 24, 45, 75, 66] | i = 2 |
| 144 | 23 | [802, 2, 24, 45, 66, 170, 75, 90] | i = 2 |
| 145 | 22 | [802, 2, 24, 2, 24, 45, 75, 66] | i = 3 |
| 146 | 23 | [802, 2, 24, 45, 66, 170, 75, 90] | i = 3 |
| 147 | 22 | [802, 2, 24, 45, 24, 45, 75, 66] | i = 4 |
| 148 | 23 | [802, 2, 24, 45, 66, 170, 75, 90] | i = 4 |
| 149 | 22 | [802, 2, 24, 45, 66, 45, 75, 66] | i = 5 |
| 150 | 23 | [802, 2, 24, 45, 66, 170, 75, 90] | i = 5 |
| 151 | 22 | [802, 2, 24, 45, 66, 170, 75, 66] | i = 6 |
| 152 | 23 | [802, 2, 24, 45, 66, 170, 75, 90] | i = 6 |
| 153 | 22 | [802, 2, 24, 45, 66, 170, 75, 66] | i = 7 |
| 154 | 23 | [802, 2, 24, 45, 66, 170, 75, 90] | i = 7 |
| 217 | 23 | [2, 24, 45, 66, 75, 90, 170, 802] | i = 0 |
| 218 | 22 | [2, 2, 24, 45, 66, 170, 75, 90] | i = 1 |
| 219 | 23 | [2, 24, 45, 66, 75, 90, 170, 802] | i = 1 |
| 220 | 22 | [2, 24, 24, 45, 66, 170, 75, 90] | i = 2 |
| 221 | 23 | [2, 24, 45, 66, 75, 90, 170, 802] | i = 2 |
| 222 | 22 | [2, 24, 45, 45, 66, 170, 75, 90] | i = 3 |
| 223 | 23 | [2, 24, 45, 66, 75, 90, 170, 802] | i = 3 |
| 224 | 22 | [2, 24, 45, 66, 66, 170, 75, 90] | i = 4 |
| 225 | 23 | [2, 24, 45, 66, 75, 90, 170, 802] | i = 4 |
| 226 | 22 | [2, 24, 45, 66, 75, 170, 75, 90] | i = 5 |
| 227 | 23 | [2, 24, 45, 66, 75, 90, 170, 802] | i = 5 |
The remaining 5 key steps follow the same pattern. Press play in the visualizer to watch every step through to the result.