希尔排序

希尔排序是在直接插入排序的基础上做的改进,也就是将要排序的序列按固定增量分成若干组,等距离者在同一组中进行直接插入排序,这里面的固定增量从n/2开始,以后每次缩小到原来的一半

#include <stdio.h>

void shsort(int s[],int n)
{
        int i,j,d;
        d = n/2;
        while(d>=1)
        {
                for(i=d+1;i<=n;i++)
                {
                        s[0] = s[i];
                        j = i - d;

                        while( (j>0) && (s[0] < s[j]))
                        {
                                s[j+d] = s[j];
                                j = j - d;
                        }
                        s[j+d] = s[0];
                }
                d = d/2;
        }
}

int main(void)
{
        int a[11],i;

        for(i=1;i<=10;i++)
        {
                scanf("%d",&a[i]);
        }
        for(i=1;i<=10;i++)
        {
                printf("%5d",a[i]);
        }
        printf("\n");

        shsort(a,10);

        for(i=1;i<=10;i++)
        {
                printf("%5d",a[i]);
        }
        printf("\n");

        return 0;
}