简单的排序算法总结

1、冒泡排序

let arr =[9,8,11,1,30]
function bubble_sort(arr){
    let swap  
    for(let i=0;i<arr.length-1;i++){
        for(let j=0;j<arr.length-i-1;j++){
            if(arr[j]>arr[j+1]){
                swap=arr[j]
                arr[j]=arr[j+1]
                arr[j+1]=swap
            }
        }
    }
}
bubble_sort(arr)   
console.log(arr) //[1, 8, 9, 11, 30]

2、快速排序

let arr =[9,8,11,1,30]
function quick_sort(arr){
    if(arr.length<=1){
        return arr;
    }
    const pivotIndex=Math.floor(arr.length/2);
    const pivot=arr.splice(pivotIndex,1)[0];

    const left=[];
    const right=[];
    for(let i=0;i<arr.length;i++){
        if(arr[i]<pivot){
            left.push(arr[i]);
        }else{
            right.push(arr[i]);
        }
    }

    return quick_sort(left).concat([pivot],quick_sort(right));
}
console.log(quick_sort(arr))   //[1, 8, 9, 11, 30]
console.log(arr) //[9, 8, 1, 30]

3、选择排序

let arr =[9,8,11,1,30]
function selection_sort(arr) {
  var len = arr.length;
  var minIndex, swap;
  for (let i = 0; i < len - 1; i++) {
    minIndex = i;
    for (let j = i + 1; j < len; j++) {
      if (arr[j] < arr[minIndex]) { //寻找最小的数
        minIndex = j; //将最小数的索引保存
      }
    }
    swap = arr[i];
    arr[i] = arr[minIndex];
    arr[minIndex] = swap;
  }
  return arr;
}
console.log(selection_sort(arr))   //[1, 8, 9, 11, 30]
console.log(arr) //[1, 8, 9, 11, 30]

4、插入排序

let arr =[9,8,11,1,30]
function insertion_sort(arr) {
  for (var i = 1; i < arr.length; i++) {
    var key = arr[i];
    var j = i - 1;
    while ( arr[j] > key) {
      arr[j + 1] = arr[j];
         j--;
    }
    arr[j + 1] = key;
  }
  return arr;
}
console.log(insertion_sort(arr))   //[1, 8, 9, 11, 30]
console.log(arr) //[1, 8, 9, 11, 30]

5、希尔排序

let arr =[9,8,11,1,30]
function shell_sort(arr) {
  var len = arr.length,
  temp,
  gap = 1;
  while(gap < len/5) { //动态定义间隔序列
    gap =gap*5+1;
  }
  for (gap; gap > 0; gap = Math.floor(gap/5)) {
    for (var i = gap; i < len; i++) {
      temp = arr[i];
      for (var j = i-gap; j >= 0 && arr[j] > temp; j-=gap) {
        arr[j+gap] = arr[j];
      }
      arr[j+gap] = temp;
    }
  }
  return arr;
}
console.log(shell_sort(arr))   //[1, 8, 9, 11, 30]
console.log(arr) //[1, 8, 9, 11, 30]

6、桶排序

//桶排序
function bucketSort(ary, bucketCount) {
  //类型检查
  if (!isArray(ary)) return false;
  if (ary.length <= 1) return ary;

  //初始化变量参数
  bucketCount = bucketCount || 5;
  var len = ary.length,
    buckets = [], //桶数组
    max = min = ary[0], //默认取出首位
    space, //数值范围
    i;

  //找出最大值最小值
  for (i = 1; i < len; i++) {
    min > ary[i] && (min = ary[i]);
    max < ary[i] && (max = ary[i]);
  }

  //求出每一个桶的数值范围(向下取值并且加一),范围取值(min -> >=max)
  space = Math.floor((max - min) / bucketCount) + 1;

  //将数值装入桶中
  for (i = 0; i < len; i++) {
    var item = ary[i], //值
      index = Math.floor((item - min) / space), //难点一,找到相应的桶序列
      bucket = buckets[index]; //桶序列数组

    //判断是否有对应桶
    if (bucket) {
      //因为数组从小到大排列
      var bucketLen = bucket.length - 1;

      //难点二,逆向比较大小,符合条件数值后移一位,k减一
      while (bucketLen >= 0 && bucket[bucketLen] > item) {
        bucket[bucketLen + 1] = bucket[bucketLen];
        bucketLen--;
      }

      //对应位置插入数组
      bucket[bucketLen + 1] = item;
    } else {
      //新增数值入桶(这裡不能引用变量bucket,详情http://www.qdfuns.com/notes/40831/dd2b82537d74065b8a53b75e2eb85715.html)
      buckets[index] = [item];
    }
  }

  //开始合并数组
  // 方法一,返回数字格式数组,但是步骤性能都较差
  /* var n = 0, result = [];
  while(n < bucketCount) {
      //中间可能有没有符合范围的断层数组
      if(buckets[n]) result = result.concat(buckets[n]);
      n++;
  }
  return result */

  //方法二,返回字符串格式数组,简单方便(平均时间快几毫秒级别)
  return buckets.join(',').split(',');
}

useTime("桶排序算法", bucketSort);

//是否数组
function isArray(obj) {
  var bol = Object.prototype.toString.call(obj) === "[object Array]";
  !bol && alert("当前入参非数组类型!");
  return bol;
}

//计时小玩意
function useTime(name, fn) {
  var ary = [
    616,
    380,
    751,
    317,
    561,
    722,
    246,
    907,
    970,
    614,
    446,
    917,
    403,
    663,
    312
  ];
  console.time(name + "耗时");
  console.log("排序前: ", ary);
  console.log("排序后: ", fn(ary));
  console.timeEnd(name + "耗时");
}

  

  

posted @ 2019-02-21 15:14  kimingw  阅读(162)  评论(0编辑  收藏  举报