zoukankan      html  css  js  c++  java
  • 例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 }
  • 相关阅读:
    Exploits Likely Leveraged by Russia’s APT28 in Highly-Targeted Attack
    WAF绕过的一些总结和思考
    PHP SAFE MODE BYPASS
    RAS算法原理
    如何绕过WAF
    360手机助手关于签名校验的分析
    cat 命令详解
    面向对象简介
    APK签名及简单反编译
    面向对象之基本概念 封装、单多继承
  • 原文地址:https://www.cnblogs.com/FWFC/p/6282491.html
Copyright © 2011-2022 走看看