zoukankan      html  css  js  c++  java
  • 例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 }
  • 相关阅读:
    79. Word Search
    97. Interleaving String
    74. Search a 2D Matrix
    73. Set Matrix Zeroes
    72. Edit Distance
    71. Simplify Path
    64. Minimum Path Sum
    shell编程 备份mysql数据库并发送到另一个服务器
    linux 命令执行的判断依据: ;,&&,||
    linux 数据流重定向
  • 原文地址:https://www.cnblogs.com/FWFC/p/6282482.html
Copyright © 2011-2022 走看看