希尔排序时插入排序的一种,是针对直接插入排序算法的改进。该方法又称缩小增量排序,因DL.Shell于1959年提出而得名
具体原理就不讲了,网上很多,现在将用c语言实现奉上:
void shell_sort(int *a,int size,int inc)
{
while (inc>=1)
{
for (int i=0;i<inc;i++)
{
for (int j=i+inc;j<size;j+=inc)
{
int temp=a[j];
int t=j;
while (t-inc>=0 && temp<a[t-inc])
{
a[t]=a[t-inc];
t=t-inc;
}
a[t]=temp;
}
}
if (inc==1)
{
return;
}
inc=inc/3+1;
}
}
void main()
{
int a[10]={3,2,1,6,7,5,8,9,0,4};
shell_sort(a,10,5);
for(int i=0;i<10;i++)
{
printf("%d\n",a[i]);
}
}