Skip to content

Instantly share code, notes, and snippets.

@flyingluscas
Created February 20, 2019 17:05
Show Gist options
  • Save flyingluscas/d274244a12d06c3fcb8ac23b335170f0 to your computer and use it in GitHub Desktop.
Save flyingluscas/d274244a12d06c3fcb8ac23b335170f0 to your computer and use it in GitHub Desktop.
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