希尔排序

2016-10-09  本文已影响0人  Neymar_

基于插入排序的希尔排序

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
上一篇 下一篇

猜你喜欢

热点阅读