经典排序——希尔排序

  感谢太原理工大学的算法演示:http://www.tyut.edu.cn/kecheng1/site01/suanfayanshi/shell_sort.asp

在希尔排序中主要是要明白在最底层是通过一次又一次的插入排序来实现的。每次都看成是h(k)个独立的数组,进行插入排序,然后循环h(k-1),h(k-2).....h(1);

代码如下:如果gap=1;那就是最后的插入排序了,不明白插入排序可以看我的插入排序

  

 1 public static void Shell(int [] test){
 2     int len = test.length;
 3     int temp,j;
 4     for(int gap=len/2;gap>0;gap=gap/2){
 5         for(int i=gap;i<len;i++){
 6             if(test[i-gap]>test[i]){//关键在于这里的控制test[i]
 7             temp = test[i];
 8         for(j=i;j>=gap&&temp<test[j-gap];j=j-gap){
 9               test[j]=test[j-gap];
10         }
11         test[j]=temp;
12             }
13     }
14             }
15 }
入门易,精通难
原文地址:https://www.cnblogs.com/chenshun-2016/p/5976559.html