1、概念
希尔排序 (Shell’s Sort)是插入排序的一种又称“缩小增量排序”(Diminishing Increment Sort),是 插入排序 算法的一种更高效的改进版本。希尔排序是非稳定排序算法。该方法因 D.L.Shell 于 1959 年提出而得名。
2、基本思想
先取一个小于n的整数d1作为第一个增量,把文件的全部记录分组。所有距离为d1的倍数的记录放在同一个组中。先在各组内进行 直接插入排序 ;然后,取第二个增量d2
般的初次取序列的一半为增量,以后每次减半,直到增量为1。
3、代码实现
public static void shellsort(int[] array)
{
int n = array.length;
int d = n / 2;
while(d > 0)
{
for(int i = d ;i < n;i++)
{
int j = i - d;
while(j >= 0 && array[j] > array[j+d])
{
int temp = array[j];
array[j] = array[j+d];
array[j+d] = temp;
j = j - d ;
}
}
d = d / 2;
}
}
public static void main(String[] args) {
int[] array = {3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};
System.out.println("数组的长度:" + array.length);
System.out.println("排序前的数组:"+Arrays.toString(array));
shellsort (array);
System.out.println("排序后的数组:"+Arrays.toString(array));
for(int i : array)
{
System.out.print( i + " ");
}
}