Files
JavaScript/Sorts/RadixSort.js
2021-05-21 11:16:11 +05:30

53 lines
1.1 KiB
JavaScript

/*
* Radix sorts an integer array without comparing the integers.
* It groups the integers by their digits which share the same
* significant position.
* For more information see: https://en.wikipedia.org/wiki/Radix_sort
*/
function radixSort (items, RADIX) {
// default radix is then because we usually count to base 10
if (RADIX === undefined || RADIX < 1) {
RADIX = 10
}
let maxLength = false
let placement = 1
while (!maxLength) {
maxLength = true
const buckets = []
for (let i = 0; i < RADIX; i++) {
buckets.push([])
}
for (let j = 0; j < items.length; j++) {
const tmp = items[j] / placement
buckets[Math.floor(tmp % RADIX)].push(items[j])
if (maxLength && tmp > 0) {
maxLength = false
}
}
let a = 0
for (let b = 0; b < RADIX; b++) {
const buck = buckets[b]
for (let k = 0; k < buck.length; k++) {
items[a] = buck[k]
a++
}
}
placement *= RADIX
}
return items
}
// Implementation of radixSort
const ar = [5, 6, 7, 8, 1, 2, 12, 14]
// Array before Sort
console.log(ar)
radixSort(ar)
// Array after sort
console.log(ar)