-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (37 loc) · 1.1 KB
/
index.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
module.exports = function lsdSort(arr){
array = arr;
function sort(index) {
var doNextRadix = false;
var i, j, k;
const currentIndexValue = 10 ** index;
const nextIndexValue = 10 ** (index + 1);
const bins = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]];
i = 0;
for (j = 0; j < array.length; j++) {
var value = array[j];
var magnitude = Math.abs(value);
if(magnitude >= nextIndexValue){
doNextRadix = true;
}
const digit = Math.floor(magnitude / currentIndexValue) % 10;
if(value >= 0){
bins[digit + 10].push(value);
}
else{
bins[10 - digit].push(value);
}
}
for (j = 0; j < bins.length; j++) {
const bin = bins[j];
for(k = 0; k < bin.length; k++){
array[i++] = bin[k];
}
}
return doNextRadix;
}
var radix = 0;
if (array.length > 1) {
while (sort(radix++));
}
return array;
}