-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounting-sort.js
46 lines (44 loc) · 1.14 KB
/
counting-sort.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
44
45
46
/**
* 计数排序
* @param {array} array
*/
function countingSort(array) {
if (array.length <= 1) return;
// 找出数组最大值
const max = findMaxValue(array);
// 数组大小为最大元素值+1
const counts = new Array(max + 1);
array.forEach((element) => {
// 元素值作为counts数组的下标来计算每个元素在array中的个数
if (!counts[element]) {
counts[element] = 0;
}
counts[element]++;
});
let sortedIndex = 0;
// 这里forEach执行的次数并不是根据counts.length的大小来的,new Array只传一个number时表示数组空间大小,
// 但是值为empty,并不会被forEach callback所执行
counts.forEach((count, i) => {
// count为元素个数, i为元素
while (count > 0) {
array[sortedIndex++] = i;
count--;
}
});
}
/**
* 依次遍历找出数组最大值,也可以Math.max(...array)
* @param {array} array
*/
function findMaxValue(array) {
let max = array[0];
for (let i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
var tmp = [4,2,6,41,6]
countingSort(tmp)
console.log(tmp);