1.原理图
2.代码
public static void main(String[] args) {
int[] array = {49, 38, 65, 97, 23, 22, 76, 1, 5, 8, 2, 0, -1, 22};
sort(array);
for (int i : array){
System.out.print(i + " ");
}
}
public static void sort(int[] array) {
int number = array.length / 2;
int i;
int j;
int temp;
while (number >= 1) {
for (i = number; i < array.length; i++) {
temp = array[i];
j = i - number;
while (j >= 0 && array[j] > temp) { //需要注意的是,这里array[j] > temp将会使数组从小到大排序。
array[j + number] = array[j];
j = j - number;
}
array[j + number] = temp;
}
number = number / 2;
}
}