希尔排序

字数 11阅读 22

基于插入排序的希尔排序

public class ShellSort {


    private static void shellSort(int[] a) {
        int N = a.length;
        int h = 1;
     
        while (h < N / 3) {
            h = h * 3 + 1;
        }
        while (h >= 1) {
            for (int i = h; i < N; i++) {
                for (int j = i; j >= h && (a[j] < a[j - h]); j -= h) {
                    int temp = a[j];
                    a[j] = a[j - h];
                    a[j - h] = temp;
                }
            }
            h = h / 3;
        }

    }

    public static void main(String[] args){
        int[] a = {10,97,863,844,202,3,49,6,39,4,72,3};
        shellSort(a);
        for(int i:a){
            System.out.print(i+", ");
        }
    }

}

2016-10-09_201224.png

推荐阅读更多精彩内容