1. Small input
Empty and one-item arrays are already sorted.
if (arr.length <= 1) return arrTyporithm · hard
Step 1 of 8 · intro card
Sorting Algorithms
Radix Sort sorts non-negative integers one digit place at a time using stable buckets.
Radix Sort avoids direct value comparisons and groups numbers by digits.
It starts at the ones place, then moves to tens, hundreds, and so on.
Each pass places values into buckets 0 through 9 and flattens those buckets back into the array.
The per-digit passes must be stable so earlier digit order is preserved while later digits are processed.
For bounded digit counts, the runtime is O(d * (n + b)), where d is digit count and b is bucket count.
Works on non-negative integer keys.
Process least significant digit first.
Use ten buckets for decimal digits.
Stable bucket order is essential.
Runtime depends on digit count, not comparisons.
function radixSort(arr) {
if (arr.length <= 1) return arr;
let max = Math.max(...arr);
let place = 1;
while (Math.floor(max / place) > 0) {
const buckets = Array.from({ length: 10 }, () => []);
for (const value of arr) {
const digit = Math.floor(value / place) % 10;
buckets[digit].push(value);
}
arr = buckets.flat();
place *= 10;
}
return arr;
}Connect each part of the code to the concept before you type.
Empty and one-item arrays are already sorted.
if (arr.length <= 1) return arrThe largest value tells us how many digit places to process.
let max = Math.max(...arr)Start sorting by the ones digit.
let place = 1Continue while the largest number still has a digit at this place.
while (Math.floor(max / place) > 0)Decimal digits need buckets 0 through 9.
Array.from({ length: 10 }, () => [])Divide by the current place and take the last digit.
const digit = Math.floor(value / place) % 10Push preserves the order of values inside each digit bucket.
buckets[digit].push(value)Buckets are concatenated back into the next partially sorted array.
arr = buckets.flat()Move from ones to tens, hundreds, and beyond.
place *= 10Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
Input[5,1,4,2,8]
n references are distributed across b = 10 digit buckets, then collected into an ordered buffer. The buckets are reused each pass. The input array is not counted as extra space.
No temporary values have been saved in this frame yet.
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state