Java实现希尔排序
程序员文章站
2022-11-24 12:52:23
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]+" "); } }
}
上一篇: 你为什么选牛肉啊?