希尔排序的实现
#include <stdio.h>
void shellSort(int a[],int length)
{
int increasement;
int i,j;
int temp;
for(increasement=length/2;increasement>0;increasement/=2) //选择步长
{
for(i=increasement;i<length;i++)
{
temp=a[i];
for(j=i-increasement;j>=0&&temp<a[j];j-=increasement) //相邻组内元素进行比较
{
a[j+increasement]=a[j]; //元素后移
}
a[j+increasement]=temp; //插入元素
}
}
}
int main()
{
int i;
printf("shell:\n");
int a[]={18, 5, 151, 138, 160, 63, 174, 169, 79, 200};
shellSort(a,10);
for(i=0;i<10;i++)
{
printf("%d ",a[i]);
}
return 0;
}