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 }
  • 相关阅读:
    CSS3--5.颜色属性
    CSS3---4.伪元素选择器
    CSS3---3.相对父元素的伪类
    CSS3---2.兄弟选择器(准确来说叫弟弟选择器,只能向下选)
    CSS3---简介与现状
    CSS3---1.属性选择器
    HTML5---22.LocalStorage的应用
    HTML5---21.SessionStorage的应用
    HTML5---19.地理定位的接口使用
    一首新裤子歌曲
  • 原文地址:https://www.cnblogs.com/FWFC/p/6282491.html
Copyright © 2011-2022 走看看