//来 自 N o w J a v a . c o m
class Shell {
public static void sort(Comparable[] a) {
int N = a.length;
int h = 1; // boundary subarray
while (h < N / 3)
h = 3 * h + 1; // 1, 4, 13, 40 ...
while (h >= 1) {
/*
*来 自
时代Java公众号 - N o w J a v a . c o m
*/
for (int i = h; i < N; i++) {
// insertion sort
for (int j = i; j >= h && less(a[j], a[j - h]); j -= h)
exch(a, j, j - h);
}
h = h / 3;
}
}
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
private static void exch(Comparable[] a, int i, int j) {
Comparable swap = a[i];
a[i] = a[j];
a[j] = swap;
}
public static void main(String[] args) {
Integer[] data = new Integer[] { 95, 29, 57, 82, 41, 83, 52, 21, 94, 79 };
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。