-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathradix_sort.js
More file actions
52 lines (49 loc) · 1.14 KB
/
radix_sort.js
File metadata and controls
52 lines (49 loc) · 1.14 KB
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
44
45
46
47
48
49
50
51
52
//基数排序,先排个位数,再排十位数,依次类推
//buckets[digit].push(number)表示digit是几,就在第几项加入数字
function radix_sort(A){
const max = Math.max(...A)
const buckets = Array.from({length:10},()=>[])
let m = 1
while(m<max){
A.forEach(number=>{
const digit = ~~ (number % (m*10)/m) //个位数排序,十位百位
buckets[digit].push(number)
})
let j = 0
buckets.forEach(bucket=>{
while(bucket.length>0){
A[j++]=bucket.shift()//个位排序,十位排序
}
})
m*=10
}
}
const A = [12,3,67,90,1,3433,222222]
radix_sort(A)
console.log(A)
//桶排序
function insertion_sort(A){
for(let i=1;i<A.length;i++){
let p = i-1
const x = A[p+1]
while(p>=0&&A[p]>x){
A[p+1] = A[p]
p--
}
A[p+1] = x
}
}
function bucket_sort(A,k,s){
const buckets = Array.from({length:10},()=>[])
//放入桶中
for(let i = 0;i<A.length;i++){
const index = ~~(A[i]/s)
buckets[index].push(A[i])
}
//排序每只桶
for(let i = 0;i<buckets.length;i++){
insertion_sort(buckets[i])
}
//取出数据
return [].concat(...buckets)
}