Chinaunix首页 | 论坛 | 博客
  • 博客访问: 42591
  • 博文数量: 20
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 200
  • 用 户 组: 普通用户
  • 注册时间: 2015-11-05 10:35
文章分类

全部博文(20)

文章存档

2016年(16)

2015年(4)

我的朋友

分类: C/C++

2016-04-13 12:56:44

希尔排序:是插入排序的一种
主要思想:将待排序列分组,然后进行插入。

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #define MAX 250

  3. int R[MAX];
  4. //某一确定增量d的希尔排序
  5. void Shell_Pass(int d,int n)
  6. {
  7.     int i,j;
  8.     for(i=d+1;i<=n;i++)
  9.     {
  10.         if(R[i]<R[i-d])
  11.         {
  12.             R[0]=R[i];
  13.             j=i-d;
  14.             do{
  15.                 R[j+d]=R[j];
  16.                 j=j-d;
  17.             }while(j>0&&R[0]<R[j]);
  18.             R[j+d]=R[0];
  19.         }
  20.     }
  21.     
  22.  }
  23.  
  24.  void Shell_Sort(int n)
  25.  {
  26.      int d=n;
  27.      do{
  28.          d=d/3+1;
  29.          Shell_Pass(d,n);
  30.      }while(d>1);
  31.  }
  32.  
  33.  int main()
  34.  {
  35.      int n,i,d;
  36.      printf("希尔排序示例:\n");
  37.      printf("Please input n above 1 and below %d\n",MAX);
  38.      scanf("%d",&n);
  39.      if(n<1||n>MAX)
  40.      {
  41.          printf("The n you input is not right!BYE!");
  42.          return 0;
  43.      }
  44.      printf("Please input the array one by one:\n");
  45.      for(i=1;i<=n;i++)
  46.      {
  47.          scanf("%d",&R[i]);
  48.      }
  49.      printf("The array you input is :\n");
  50.      for(i=1;i<=n;i++)
  51.      {
  52.          printf("%d ",R[i]);
  53.      }
  54.      Shell_Sort(n);
  55.      printf("The array after Shell order is:\n");
  56.      for(i=1;i<=n;i++)
  57.      {
  58.          printf("%d ",R[i]);
  59.      }
  60.      return 0;
  61.  }

阅读(1124) | 评论(0) | 转发(0) |
0

上一篇:直接插入排序

下一篇:快速排序

给主人留下些什么吧!~~