步长分组的插入排序
public class ShellSort {
public static void main(String[] args) {
int[] data = {15, 9, 7, 8, 20, -1, 4};
shellSort(data);
}
private static void shellSort(int[] data) {
int n = data.length;
int k = n;
int[] delta = new int[n >> 1];
int i = 0;
do {
k >> 1;
delta[i++] = k;
} while (k > 1);
i = 0;
int dk;
while ((dk = delta[i]) > 0) {//步长
for (k = delta[i]; k < n; k++) {
if (data[k - dk] > data[k]) {
int t = data[k];
int j;
for (j = k - dk; j >= 0 && data[j] > t; j -= dk) {
data[j + dk] = data[j];
}
data[j + dk] = t;
}
}
i++;
}
}
}
x >> 1 等同于 x /= 2 且位运算效率更高