forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadixSort.js
43 lines (38 loc) · 1011 Bytes
/
RadixSort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* 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
*/
export 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
}