// 计数排序(0,正整数)
function countingSort(arr) {
var len = arr.length;
var maxNum = Math.max.apply({}, arr);
var bucket = Array(maxNum + 1).fill(0);
var newArr = [];
for(var i = 0; i < len; i++) {
bucket[arr[i]]++;
}
for(var j = 0; j <= maxNum; j++) {
while(bucket[j] > 0) {
newArr.push(j);
bucket[j]--;
}
}
return newArr;
}
countingSort.gif
网友评论