整数排序 0-99 最好的算法
1 把排序元素的值 当 tmpArr数组的索引, 计数出现次数,
2 遍历 tmpArr, 按照 value 值的个数, unshift|push tmpArr 索引值 到 newArr 完成排序
'use strict'
var arr = [0, 0, 1, 2, 3, 4, 5, 10, 12, 22, 123, 333, 333, 444, 555, 666, 777, 888, 999, 1000];
console.log(arr.length)
function countionSort(arr) {
var countArr = [];
var count = 0; // 辅助计数
// 把原始数组的元素, 当做索引值, 记录出现的次数
arr.forEach(function (value) {
if (!countArr[value]) {
countArr[value] = 1;
} else {
countArr[value] += 1;
}
count += 1;
});
// 按照出现的次数, 把索引值 当做新数组的 value, unshift|push 正序|反序
var newArr = [];
countArr.forEach(function (value, index) {
if (value) {
while (value) {
// newArr.push(index);
newArr.unshift(index);
value--;
count += 1;
}
}
});
console.log(count);
return newArr;
}
console.log(countionSort(arr));