例19:直接插入排序

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