java实现希尔排序算法
源代码
public class ShellSort extends DataCrol {
@Override
public void sort(int[] array) {
int h = 0;
int size = array.length;
while (h <= size) {
h = 3 * h + 1;
}
while (h > 0) {
for (int i = h; i < size; i++) {
int j = i - h;
int get = array[i];
while (j >= 0 && array[j] > get) {
array[j + h] = array[j];
j = j - h;
}
array[j + h] = get;
}
h = (h - 1) / 3;
}
}
public static void main(String[] args) {
ShellSort insertionSort = new ShellSort();
int[] array = DataCrol.createRandomArray(20);
DataCrol.print(array);
insertionSort.sort(array);
DataCrol.print(array);
insertionSort.timeTest(10000000);
}
}