希尔排序

package cn.aust.zyw.demo;

/**
 * Created by zyw on 2016/2/9.
 */
public class Shell {
    public static void sort(int a[]){
        int N=a.length;
        int h=1;
        while(h<N/4) h=4*h+1;//divide in four
        while (h>=1){
            for(int i=h;i<N;i++){
                for(int j=i;j>=h&&less(a[j],a[j-h]);j-=h)
                    exch(a,j,j-h);
            }
            h=h/4;
        }
    }
    private static boolean less(int  v, int w){
        if(v<w) return true;
        return false;
    }
    private static void exch(int[] a,int i,int j){
        int t=a[i];a[i]=a[j];a[j]=t;
    }
    private static void show(int[] a){
        for(int i=0;i<a.length;i++){
            System.out.printf(a[i]+" ");
        }
    }
    public static boolean isSorted(int[] a){
        for(int i=1;i<a.length;i++)
            if(less(a[i],a[i-1])) return false;
        return true;
    }
    public static  void main(String args[]){
        int a[]={20,5,14,3,63,1,55,11,0};
        sort(a);
        show(a);
    }
}

 

你可能感兴趣的:(希尔排序)