public class ShellSort { public static void main(String[] args) { int A[] = { 5, 2, 9, 4, 7, 6, 1, 3, 8 };// 从小到大希尔排序 sort(A,A.length); for (int i = 0; i < A.length; i++) { System.out.printf("%d ", A[i]); } } public static void sort(int A[], int n){ int h = 0; while (h <= n) // 生成初始增量 { h = 3 * h + 1; } while (h >= 1) { for (int i = h; i < n; i++) { int j = i - h; int get = A[i]; while (j >= 0 && A[j] > get) { A[j + h] = A[j]; j = j - h; } A[j + h] = get; } h = (h - 1) / 3; // 递减增量 } } }