欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现希尔排序

程序员文章站 2022-05-20 18:01:57
public class ShellSort { public static void shellSort(int[] list){ int d = list.length; int temp = 0; while(true){ d = (int)Math.ceil(d/2);//保证向上取整 的d ......

public class ShellSort {

    public static void shellSort(int[] list){
    int d = list.length;
    int temp = 0;
    while(true){
        d = (int)Math.ceil(d/2);//保证向上取整  的d1 为double
        //int d = (int)d1;
        for(int x=0;x<d;x++){
            //插入排序
            for(int i=x+d;i<list.length;i+=d){
                int j = i - d;//j定义在外面  因为最后要完成交换
                temp = list[i];
                for(;j>=0&&temp<list[j];j-=d){
                    list[j+d] = list[j];
                }
                list[j+d] = temp;
            }
        }
        if(d==1)
            break;
    }
}
public static void main(String[] args) {
    int [] list = {3,2,1,4,5,8,9};
    shellSort(list);
    for(int i=0;i<list.length;i++){
        System.out.print(list[i]+" ");
    }

}

}