package 希尔; import java.util.Arrays; public class ShellSort { public static void sort(int[] a){ int d = a.length; while (d!=1){ d=d/2; System.out.println(d); for(int i=d; i<a.length; i++){ //分组 int temp = a[i]; int tempdex= i; while(tempdex >= d && a[tempdex-d] > temp){ a[tempdex] = a[tempdex - d]; tempdex = tempdex - d; } a[tempdex] = temp; } } } public static void main(String[] args) { int[] a = new int[] {8,7,6,5,4,3,2,1}; sort(a); System.out.println(Arrays.toString(a)); } }
希尔排序 while循环方式
最新推荐文章于 2024-04-27 11:13:02 发布