美文网首页
十大排序算法(JS)

十大排序算法(JS)

作者: 简栋梁 | 来源:发表于2019-04-25 23:58 被阅读0次

    目录
    一、冒泡排序
    二、选择排序
    三、插入排序
    四、希尔排序
    五、归并排序
    六、快速排序
    七、堆排序
    八、计数排序
    九、桶排序
    十、基数排序

    系列教程
    十大排序算法动画

    一、冒泡排序

    function bubbleSort3(arr3) {
      var low = 0;
      var high= arr.length-1; //设置变量的初始值
      var tmp,j;
      console.time('2.改进后冒泡排序耗时');
      while (low < high) {
        for (j= low; j< high; ++j) {         //正向冒泡,找到最大者
          if (arr[j]> arr[j+1]) {
            tmp = arr[j]; arr[j]=arr[j+1];arr[j+1]=tmp;
          }
        }
        --high;  //修改high值, 前移一位
        for (j=high; j>low; --j) {          //反向冒泡,找到最小者
          if (arr[j]<arr[j-1]) {
            tmp = arr[j]; arr[j]=arr[j-1];arr[j-1]=tmp;
          }
        } 
        ++low;  //修改low值,后移一位
      }
      console.timeEnd('2.改进后冒泡排序耗时');
      return arr3;
    }
    var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48];
    console.log(bubbleSort3(arr));//[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50] ;
    

    二、选择排序

    function selectionSort(arr) {
      var len = arr.length;
      var minIndex, temp;
      console.time('选择排序耗时');
      for (var i = 0; i < len - 1; i++) {
        minIndex = i;
        for (var j = i + 1; j < len; j++) {
          if (arr[j] < arr[minIndex]) { //寻找最小的数
            minIndex = j; //将最小数的索引保存
          }
        }
        temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
      }
      console.timeEnd('选择排序耗时');
      return arr;
    }
    var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48];
    console.log(selectionSort(arr));//[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50];
    

    三、插入排序

    function binaryInsertionSort(array) {
      console.time('二分插入排序耗时:');
      for (var i = 1; i < array.length; i++) {
        var key = array[i], left = 0, right = i - 1;
        while (left <= right) {
          var middle = parseInt((left + right) / 2);
          if (key < array[middle]) {
            right = middle - 1;
          } else {
            left = middle + 1;
          }
        }
        for (var j = i - 1; j >= left; j--) {
          array[j + 1] = array[j];
        }
        array[left] = key;
      }
      console.timeEnd('二分插入排序耗时:');
      return array;
    }
    var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48];
    console.log(binaryInsertionSort(arr));//[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50];
    

    四、希尔排序

    function shellSort(arr) {
      var len = arr.length,
      temp,
      gap = 1;
      console.time('希尔排序耗时:');
      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;
        }
      }
      console.timeEnd('希尔排序耗时:');
      return arr;
    }
    var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48];
    console.log(shellSort(arr));//[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50];
    

    五、归并排序

    function mergeSort(arr) { //采用自上而下的递归方法
      var len = arr.length;
      if(len < 2) {
        return arr;
      }
      var middle = Math.floor(len / 2),
      left = arr.slice(0, middle),
      right = arr.slice(middle);
      return merge(mergeSort(left), mergeSort(right));
    }
     
    function merge(left, right){
      var result = [];
      console.time('归并排序耗时');
      while (left.length && right.length) {
        if (left[0] <= right[0]) {
          result.push(left.shift());
        } else {
          result.push(right.shift());
        }
      }
     
      while (left.length){
        result.push(left.shift());
      }
      while (right.length){
        result.push(right.shift());
      }
      console.timeEnd('归并排序耗时');
      return result;
    }
    var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48];
    console.log(mergeSort(arr));
    

    六、快速排序

    var quickSort2 = function(arr) {
      console.time('2.快速排序耗时');
      if (arr.length <= 1) { return arr; }
      var pivotIndex = Math.floor(arr.length / 2);
      var pivot = arr.splice(pivotIndex, 1)[0];
      console.log(pivot)
      var left = [];
      var right = [];
      for (var i = 0; i < arr.length; i++){
        if (arr[i] < pivot) {
          left.push(arr[i]);
        } else {
          right.push(arr[i]);
        }
      }
      console.timeEnd('2.快速排序耗时');
      return quickSort2(left).concat([pivot], quickSort2(right));
    };
    var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48];
    console.log(quickSort2(arr));//[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50];
    

    七、堆排序

    function heapSort(array) {
      console.time('堆排序耗时');
      //建堆
      var heapSize = array.length, temp;
      for (var i = Math.floor(heapSize / 2) - 1; i >= 0; i--) {  
        heapify(array, i, heapSize);
      }
      //堆排序
      for (var j = heapSize - 1; j >= 1; j--) {
        temp = array[0];
        array[0] = array[j];
        array[j] = temp;
        console.log(array)
        heapify(array, 0, --heapSize);
      }
      console.timeEnd('堆排序耗时');
      return array;
    }
    function heapify(arr, x, len) {
      var l = 2 * x + 1, r = 2 * x + 2, largest = x, temp;
      if (l < len && arr[l] > arr[largest]) {
        largest = l;
      }
      if (r < len && arr[r] > arr[largest]) {
        largest = r;
      }
      if (largest != x) {
        temp = arr[x];
        arr[x] = arr[largest];
        arr[largest] = temp;
        console.log(arr)
        heapify(arr, largest, len);
      }
    }
    var arr=[91,60,96,13,35,65,46,65,10,30,20,31,77,81,22];
    console.log(heapSort(arr));//[10, 13, 20, 22, 30, 31, 35, 46, 60, 65, 65, 77, 81, 91, 96];
    

    八、计数排序

    function countingSort(array) {
      var len = array.length,
      B = [],
      C = [],
      min = max = array[0];
      console.time('计数排序耗时');
      for (var i = 0; i < len; i++) {
        min = min <= array[i] ? min : array[i];
        max = max >= array[i] ? max : array[i];
        C[array[i]] = C[array[i]] ? C[array[i]] + 1 : 1;
        console.log(C)
      }
    
      // 计算排序后的元素下标
      for (var j = min; j < max; j++) {
        C[j + 1] = (C[j + 1] || 0) + (C[j] || 0);
        console.log(C)
      }
      for (var k = len - 1; k >= 0; k--) {
        B[C[array[k]] - 1] = array[k];
        C[array[k]]--;
        console.log(B)
      }
      console.timeEnd('计数排序耗时');
      return B;
    }
    var arr = [2, 2, 3, 8, 7, 1, 2, 2, 2, 7, 3, 9, 8, 2, 1, 4, 2, 4, 6, 9, 2];
    console.log(countingSort(arr)); //[1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 6, 7, 7, 8, 8, 9, 9];
    

    九、桶排序

    function bucketSort(array, num) {
      if (array.length <= 1) {
        return array;
      }
      var len = array.length, buckets = [], result = [], min = max = array[0], space, n = 0;
    
      var index = Math.floor(len / num) ;
      while(index<2){
    
        num--;
        index = Math.floor(len / num) ;
      }
    
      console.time('桶排序耗时');
      for (var i = 1; i < len; i++) {
        min = min <= array[i] ? min : array[i];
        max = max >= array[i] ? max : array[i];
      }
      space = (max - min + 1) / num;  //步长
      for (var j = 0; j < len; j++) {
        var index = Math.floor((array[j] - min) / space);
        if (buckets[index]) { // 非空桶,插入排序
          var k = buckets[index].length - 1;
          while (k >= 0 && buckets[index][k] > array[j]) {
            buckets[index][k + 1] = buckets[index][k];
            k--;
          }
          buckets[index][k + 1] = array[j];
        } else { //空桶,初始化
          buckets[index] = [];
          buckets[index].push(array[j]);
        }
      }
      while (n < num) {
        result = result.concat(buckets[n]);
        n++;
      }
      console.timeEnd('桶排序耗时');
      return result;
    }
    var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48];
    console.log(bucketSort(arr,4));//[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50];
    

    十、基数排序

    function radixSort(arr, maxDigit) {
      var mod = 10;
      var dev = 1;
      var counter = [];
      console.time('基数排序耗时');
      for (var i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {
        for(var j = 0; j < arr.length; j++) {
          var bucket = parseInt((arr[j] % mod) / dev);
          if(counter[bucket]== null) {
            counter[bucket] = [];
          }
        counter[bucket].push(arr[j]);
        }
        var pos = 0;
        for(var j = 0; j < counter.length; j++) {
          var value = null;
          if(counter[j]!=null) {
            while ((value = counter[j].shift()) != null) {
              arr[pos++] = value;
            }
          }
        }
      }
      console.timeEnd('基数排序耗时');
      return arr;
    }
    var arr = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48];
    console.log(radixSort(arr,2)); //[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50];
    

    相关文章

      网友评论

          本文标题:十大排序算法(JS)

          本文链接:https://www.haomeiwen.com/subject/ofcxnqtx.html