希尔排序
先取一个小于n的整数d1作为第一个增量
,把文件的全部记录分组。所有距离为d1的倍数的记录放在同一个组中。先在各组内进行直接插入排序
;然后,取第二个增量d2<d1重复上述的分组和排序,直至所取的增量
=1(
<
…<d2<d1),即所有记录放在同一组中进行直接插入排序为止。
![](https://i-blog.csdnimg.cn/blog_migrate/8ddb2da958b9ae1a32e7b82bcc82a379.png)
![](https://i-blog.csdnimg.cn/blog_migrate/70393acd7fb00f878e3a27c297c83bd2.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ead414f499c9e9e3a15cff6688caf059.png)
# include <stdio.h>
void HillSort(int *num, int length){
int i, j, temp, gap, t;
for(gap = length/2; gap>0; gap/=2){//确定增量
for(i=0; i<=gap; i++){//确定分组
for(j = i+gap; j<length; j+=gap){//进行插入排序
if(num[j-gap]>num[j]){
temp = num[j];
t = j-gap;
while(t>=0&& num[t]>temp){
num[t+gap] = num[t];
t-=gap;
}
num[t+gap] = temp;
}
}
}
}
return ;
}
int main(void){
int num[]={9,8,7,6,5,4,3,2,1,0};
int length = 10;
HillSort(num, length);
for(int i = 0 ;i < 10; i++){
printf("%d ", num[i]);
}
printf("\n");
return 0;
}