例20:希尔排序

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 void ShellSort(int *pArray,int nCount)
 5 {
 6      int gap = nCount/2;
 7      while(gap >= 1)
 8      {
 9                for(int i = gap; i<nCount;i++)
10                {
11                        int j = 0;
12                        int tmp = pArray[i];
13                        for(j = i-gap; j>=0&&tmp<pArray[j]; j-=gap)
14                        {
15                                pArray[j+gap] = pArray[j];
16                        }
17                        pArray[j+gap] = tmp;
18                }
19                gap /= 2;
20      }
21 }
22 
23 int main()
24 {
25     int nCount,pArray[20];
26     while(~scanf("%d",&nCount)&&nCount)
27     {
28                                        for(int i = 0;i<nCount&&i<20;i++)
29                                        {
30                                                scanf("%d",&pArray[i]);
31                                        }
32                                        ShellSort(pArray,nCount);
33                                        for(int i = 0;i<nCount&&i<20;i++)
34                                        {
35                                                printf("%d ",pArray[i]);
36                                        }
37                                        printf("
");
38     }
39     return 0;
40 }
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 void ShellSort(int *pArray,int nCount)
 5 {
 6      int gap = nCount/2;
 7      while(gap >= 1)
 8      {
 9                for(int i = gap; i<nCount;i++)
10                {
11                        int j = 0;
12                        int tmp = pArray[i];
13                        for(j = i-gap; j>=0&&tmp<pArray[j]; j-=gap)
14                        {
15                                pArray[j+gap] = pArray[j];
16                        }
17                        pArray[j+gap] = tmp;
18                }
19                gap /= 2;
20      }
21 }
22 
23 int main()
24 {
25     int nCount,pArray[20];
26     while(~scanf("%d",&nCount)&&nCount)
27     {
28                                        for(int i = 0;i<nCount&&i<20;i++)
29                                        {
30                                                scanf("%d",&pArray[i]);
31                                        }
32                                        ShellSort(pArray,nCount);
33                                        for(int i = 0;i<nCount&&i<20;i++)
34                                        {
35                                                printf("%d ",pArray[i]);
36                                        }
37                                        printf("
");
38     }
39     return 0;
40 }
原文地址:https://www.cnblogs.com/FWFC/p/6282491.html