排序——希尔排序

找一个gap,在arrays中,以gap为间隔,分小组,对每一组数据其进行插入排序

然后,减小gap,再依次进行插入排序

注意:其中控制大循环的增量i++,不是i += gap,因为如果是i+=gap的话,就是按照以gap为间隔只排0, gap, 2gap......,其他的1,1+gap,1+2gap......不会排序

public class ShellSort {
    public void shellSort(Integer[] arrays){
        if(arrays.length == 0) return;
        int len = arrays.length, j;
        for(int gap = len / 2; gap > 0; gap /= 2){
            for(int i = gap; i < len; i++){
                int temp = arrays[i];
                for(j = i - gap; j >= 0; j = j - gap){
                    if(arrays[j] < temp) break;
                    arrays[j + gap] = arrays[j];
                }
                arrays[j + gap] = temp;
            }
        }
    }
}

  

posted @ 2018-03-27 21:47  SkyeAngel  阅读(140)  评论(0编辑  收藏  举报