forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.js
More file actions
Latest commit
43 lines (38 loc) · 1015 Bytes
/
Copy pathRadixSort.js
File metadata and controls
43 lines (38 loc) · 1015 Bytes
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
*/
exportfunctionradixSort(items,RADIX){
// default radix is then because we usually count to base 10
if(RADIX===undefined||RADIX<1){
RADIX=10
}
letmaxLength=false
letplacement=1
while(!maxLength){
maxLength=true
constbuckets=[]
for(leti=0;i<RADIX;i++){
buckets.push([])
}
for(letj=0;j<items.length;j++){
consttmp=items[j]/placement
buckets[Math.floor(tmp%RADIX)].push(items[j])
if(maxLength&&tmp>0){
maxLength=false
}
}
leta=0
for(letb=0;b<RADIX;b++){
constbuck=buckets[b]
for(letk=0;k<buck.length;k++){
items[a]=buck[k]
a++
}
}
placement*=RADIX
}
returnitems
}