标签:
package 备份类; public class 希尔直插排序 { public static void main(String[] args) { int[] a = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 10 }; print(a); shellSort(a); print(a); } public static void shellSort(int[] a) { int d = 3;// 增量 int gap = 1;// 间距 // 计算出最大的gap值 while (gap <= a.length / d) { gap = gap * d + 1; } // 组间的循环 while (gap > 0) { // 一组内的循环 for (int i = gap; i < a.length; i += gap) { if (a[i - gap] > a[i]) { int tmp = a[i]; int j = i - gap; while (j >= 0 && a[j] > tmp) {// 开始移动了 a[j + gap] = a[j]; j -= gap; } a[j + gap] = tmp; print(a); } } // 计算出下一个gap值,开始下一组排序,下一组gap减小 gap = (gap - 1) / d; } } public static void print(int[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + "\t"); } System.out.println(); } }
标签:
原文地址:http://www.cnblogs.com/hest0001/p/5264675.html