希爾排序

#include <stdio.h>
int shsort(int s[], int n)    /* 自定義函數 shsort()*/
{
    int i,j,d;
    d=n/2;    /*肯定固定增雖值*/
    while(d>=1)
    {
        for(i=d+1;i<=n;i++)    /*數組下標從d+1開始進行直接插入排序*/
        {
            s[0]=s[i];    /*設置監視哨*/
            j=i-d;    /*肯定要進行比較的元素的最右邊位置*/
            while((j>0)&&(s[0]<s[j]))
            {
                s[j+d]=s[j];    /*數據右移*/
                j=j-d;    /*向左移d個位置V*/
            }
            s[j + d]=s[0];    /*在肯定的位罝插入s[i]*/
        }
        d = d/2;    /*增裏變爲原來的一半*/
    }
return 0;
}
int main()
{
    int a[11],i;    /*定義數組及變量爲基本整型*/
    printf("請輸入 10 個數據:\n");
    for(i=1;i<=10;i++)
    scanf("%d",&a[i]);    /*從鍵盤中輸入10個數據*/
    shsort(a, 10);    /* 調用 shsort()函數*/
    printf("排序後的順序是:\n");
    for(i=1;i<=10;i++)
    printf("%5d",a[i]);    /*輸出排序後的數組*/
    printf("\n");
    return 0;
}數組

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息