Skip to main content

Radix Sort

Radix sort - Wikipedia

radix sort

Understanding Radix Sort Through JavaScript | DigitalOcean

const radixSort = arr => {
let maxLength = largestNum(arr);

for (let i = 0; i < maxLength; i++) {
let buckets = Array.from({ length: 10 }, () => []);

for (let j = 0; j < arr.length; j++) {
let num = getNum(arr[j], i);

if (num !== undefined) buckets[num].push(arr[j]);
};
arr = buckets.flat();
};
return arr;
};

const largestNum = arr => {
let largest = "0";

arr.forEach(num => {
const strNum = String(num);

if (strNum.length > largest.length) largest = strNum;
});

return largest.length;
};