@java
数据结构
希尔排序
public class Shell {
public static void sort(Comparable[] a) {
int h = 1;
while (h < a.length) {
h = h * 2 + 1;
}
while (h >= 1) {
for (int i = h; i < a.length; i++) {
for (int j = i; j >= h; j -= h) {
if (judge(a[j - h], a[j])) {
exchange(a, j - h, j);
} else {
break;
}
}
}
h /= 2;
}
}
public static boolean judge(Comparable s1, Comparable s2) {
return s1.compareTo(s2) > 0;
}
public static void exchange(Comparable[] a, int i, int j) {
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}