计数排序

  1. var arr = [4,4,6,5,3,2,8,1,7,5,6,0,10];
  2. countSort(arr);
  3. function countSort(arr) {
  4. const __list = [];
  5. let max = arr[0];
  6. for (let i = 0; i < arr.length; i++) {
  7. if (max < arr[i]) {
  8. max = arr[i]
  9. }
  10. }
  11. __list.length = max + 1;
  12. __list.fill(0);
  13. for (let i = 0; i < arr.length; i++) {
  14. __list[arr[i]]++;
  15. }
  16. console.log(__list);
  17. return __list;
  18. }
  19. VM3229:20 (11) [1, 1, 1, 1, 2, 2, 2, 1, 1, 0, 1]

计数排序优化

  1. var arr = [90,99,95,94,95];
  2. countSort(arr);
  3. function countSort(arr) {
  4. const __list = [];
  5. let max = arr[0];
  6. let min = arr[0];
  7. for (let i = 0; i < arr.length; i++) {
  8. if (max < arr[i]) {
  9. max = arr[i];
  10. }
  11. if (min > arr[i]) {
  12. min = arr[i];
  13. }
  14. }
  15. const len = max - min;
  16. debugger;
  17. __list.length = len + 1;
  18. __list.fill(0);
  19. for (let i = 0; i < arr.length; i++) {
  20. __list[arr[i] - min]++;
  21. }
  22. console.log(__list);
  23. return __list;
  24. }
  25. /*
  26. 99 - 90 = 9
  27. [1, 0, 0, 0, 1, 2, 0, 0, 0, 1]
  28. */
  1. var arr = [90,99,95,94,95];
  2. countSort(arr);
  3. function countSort(arr) {
  4. const __list = [];
  5. let max = arr[0];
  6. let min = arr[0];
  7. for (let i = 0; i < arr.length; i++) {
  8. if (max < arr[i]) {
  9. max = arr[i];
  10. }
  11. if (min > arr[i]) {
  12. min = arr[i];
  13. }
  14. }
  15. const len = max - min;
  16. __list.length = len + 1;
  17. __list.fill(0);
  18. for (let i = 0; i < arr.length; i++) {
  19. __list[arr[i] - min]++;
  20. }
  21. for (let i = 1; i < __list.length; i++) {
  22. __list[i] += __list[i - 1];
  23. }
  24. let __newList = [];
  25. __newList.length = arr.length;
  26. for (let i = arr.length - 1; i >= 0; i--) {
  27. __newList[__list[arr[i] - min] - 1] = arr[i]
  28. __list[arr[i] - min] -= 1;
  29. }
  30. console.log(__newList);
  31. return __newList;
  32. }
  33. // VM3831:35 (5) [90, 94, 95, 95, 99]

桶排序

区间跨度 = (最大值 - 最小值) / (桶的数量 - 1)
1.获取最大值max,最小值min,差值d
2.初始化桶,桶的长度 = arr.length
bucketNum = arr.length
bucketArr = [];
for (let i = 0; i < bucketNum; i++) bucketArr.push([]);
3.遍历元素数组,将每个元素加入对应的桶中
const num = parseInt((max - min) / (bucketNum - 1));
bucketArr[num].push(arr[i])
4.对每个桶内部进行排序 Timsort
bucketArr[i].sort()
5.输出 …

function sort(arr) {
  let max = arr[0];
  let min = arr[0];
  for (let i = 0; i < arr.length; i++) {
    if (max < arr[i]) {
       max = arr[i];
    }
    if (min > arr[i]) {
      min = arr[i];
    }
  }
  const d = max - min;
  const bucketNum = arr.length
  const bucketArr = [];
  for (let i = 0; i < bucketNum; i++)
    bucketArr.push([]);
  for (let i = 0; i < arr.length; i++) {
    const num = parseInt((max - min) / (bucketNum - 1));
    bucketArr[num].push(arr[i])
  }
  for (let i = 0; i < arr.length; i++) {
    bucketArr[i].sort(); // 采用Timsort排序
  }
  const sortArr = [];
  for (let i = 0; i < bucketArr.length; i++) {
    for (let j = 0; j < bucketArr[i].length; j++) {
      sortArr.push(bucketArr[i][j])
    }
  }
  console.log(sortArr);
  return sortArr;
}

var arr = [0.84, 4.5, 2.18, 0.5, 3.25]
sort(arr);
// VM3938:30 (5) [0.5, 0.84, 2.18, 3.25, 4.5]