Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Radix Sort WIP
const unsortedArray = [123, 49, 7, 333]
const radix = (arr) => {
const max = Math.max(...arr).toString().length
const buckets = new Array(10)
let arrayWithLeftPaddedZeros = []
for (let i = 0; i < arr.length; i++) {
arrayWithLeftPaddedZeros[i] = arr[i].toString().padStart(max, '0')
}
for (let i = 0; i < arrayWithLeftPaddedZeros.length; i++) {
let j = 2
//for (let j = max - 1; j >= 0; j--) {
if (!buckets[arrayWithLeftPaddedZeros[i][j]]) {
buckets[arrayWithLeftPaddedZeros[i][j]] = []
}
buckets[arrayWithLeftPaddedZeros[i][j]].push(arrayWithLeftPaddedZeros[i])
// }
}
const a = [...buckets]
console.log(buckets.toString())
}
radix(unsortedArray)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.