- 基本思想:先取一个小于n的整数d1作为第一个增量,把文件的全部记录分组。所有距离为d1的倍数的记录放在同一个组中。先在各组内进行直接插入排序;然后,取第二个增量d2 重复上述的分组和排序,直至所取的增量 =1,即所有记录放在同一组中进行直接插入排序为止。
package SortTest;
import java.util.Scanner;
public class shellSort {
/**
* 希尔排序
*
* @param args
*/
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
int count = sca.nextInt();
int[] a = new int[count];
for (int i = 0; i < count; i++) {
a[i] = sca.nextInt();
}
int i, j, temp;
int h = 1;
while (h < a.length / 3) {
h = h * 3 + 1;
}
while (h > 0) {
for (i = h; i < a.length; i++) {
temp = a[i];
j = i;
while (j > h - 1 && a[j - h] >= temp) {
a[j] = a[j - h];
j -= h;
}
a[j] = temp;
}
h = (h - 1) / 3;
}
for (int x = 0; x < a.length; x++) {
System.out.print(a[x] + " ");
}
}
}