zoukankan      html  css  js  c++  java
  • js希尔排序

    1959年Shell发明,第一个突破O(n2)的排序算法,是简单插入排序的改进版。它与插入排序的不同之处在于,它会优先比较距离较远的元素。希尔排序又叫缩小增量排序。

    算法描述

    先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,具体算法描述:

    • 选择一个增量序列t1,t2,…,tk,其中ti>tj,tk=1;
    • 按增量序列个数k,对序列进行k 趟排序;
    • 每趟排序,根据对应的增量ti,将待排序列分割成若干长度为m 的子序列,分别对各子表进行直接插入排序。仅增量因子为1 时,整个序列作为一个表来处理,表长度即为整个序列的长度。

    动图演示

    代码实现

     1 let array = randomArray(1,100);
     2 console.log(array);
     3 let stepSequnece = getStepSequnece(array);
     4 for (let i = 0;i < stepSequnece.length;i++) {
     5     shellSort(stepSequnece[i]);
     6 }
     7 console.log(array);
     8 function shellSort(step) {
     9     for (let col = 0;col < step;col++) {
    10         for (let begin = col + step;begin < array.length;begin +=step) {
    11             let cur = begin;
    12         while(cur > col && array[cur] - array[cur-step] < 0) {
    13         let temp = array[cur];
    14         array[cur] = array[cur-step];
    15         array[cur-step] = temp;
    16         cur -= step;
    17         }
    18         }
    19     }
    20 }
    21             
    22 function getStepSequnece(array) {
    23     let stepSequnece = [];
    24     let step = array.length;
    25     while((step >>= 1) > 0) {
    26         stepSequnece.push(step);
    27     }
    28     return stepSequnece;
    29 }    
    30 function randomArray(start,end){
    31     var a=[],o={},random,step=end-start;
    32     while(a.length<step){
    33         random=start+parseInt(Math.random()*step);
    34         if(!o["x"+random]){
    35             a.push(random);
    36             o["x"+random]=1;
    37         };
    38     };
    39     return a;
    40 };

    算法分析

    希尔排序的核心在于间隔序列的设定。既可以提前设定好间隔序列,也可以动态的定义间隔序列。

  • 相关阅读:
    istio kiali 亲和性调度
    istio kiali 内部介绍
    istio kiali 可视化bookinfo
    Istio 部署Bookinfo 应用
    初探istio kiali
    安装Istio
    Istio 是什么?
    AQS源码浅析
    go 单元测试testify
    go unknown revision报错
  • 原文地址:https://www.cnblogs.com/ming1025/p/13883237.html
Copyright © 2011-2022 走看看