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 }
  • 相关阅读:
    W3C规范
    背景图片调整大小
    comfirm和prompt的区别
    position属性absolute与relative 的区别
    text-decoration和text-indent和text-shadow
    刷新网页跳转锚点
    安卓中location.href或者location.reload 不起作用
    $_SERVER 当前信息
    堆+思维——cf1330E
    树形dp——cf1332F【好题】
  • 原文地址:https://www.cnblogs.com/FWFC/p/6282491.html
Copyright © 2011-2022 走看看