第十五周项目一(2)希尔排序

1.希尔排序

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #define MaxSize 20  
  3. typedef int KeyType;    //定义关键字类型  
  4. typedef char InfoType[10];  
  5. typedef struct          //记录类型  
  6. {  
  7.     KeyType key;        //关键字项  
  8.     InfoType data;      //其他数据项,类型为InfoType  
  9. } RecType;              //排序的记录类型定义  
  10.   
  11. void ShellSort(RecType R[],int n)   //希尔排序算法  
  12. {  
  13.     int i,j,gap;  
  14.     RecType tmp;  
  15.     gap=n/2;                //增量置初值  
  16.     while (gap>0)  
  17.     {  
  18.         for (i=gap; i<n; i++) //对所有相隔gap位置的所有元素组进行排序  
  19.         {  
  20.             tmp=R[i];  
  21.             j=i-gap;  
  22.             while (j>=0 && tmp.key<R[j].key)//对相隔gap位置的元素组进行排序  
  23.             {  
  24.                 R[j+gap]=R[j];  
  25.                 j=j-gap;  
  26.             }  
  27.             R[j+gap]=tmp;  
  28.             j=j-gap;  
  29.         }  
  30.         gap=gap/2;  //减小增量  
  31.     }  
  32. }  
  33.   
  34. int main()  
  35. {  
  36.     int i,n=11;  
  37.     RecType R[MaxSize];  
  38.     KeyType a[]= {16,25,12,30,47,11,23,36,9,18,31};  
  39.     for (i=0; i<n; i++)  
  40.         R[i].key=a[i];  
  41.     printf("排序前:");  
  42.     for (i=0; i<n; i++)  
  43.         printf("%d ",R[i].key);  
  44.     printf("\n");  
  45.     ShellSort(R,n);  
  46.     printf("排序后:");  
  47.     for (i=0; i<n; i++)  
  48.         printf("%d ",R[i].key);  
  49.     printf("\n");  
  50.     return 0;  

  1. }  

  2. 2.排序中输出每一趟的中间结果

    [cpp]  view plain  copy
      在CODE上查看代码片 派生到我的代码片
    1. #include <stdio.h>  
    2. #define MaxSize 20  
    3. typedef int KeyType;    //定义关键字类型  
    4. typedef char InfoType[10];  
    5. typedef struct          //记录类型  
    6. {  
    7.     KeyType key;        //关键字项  
    8.     InfoType data;      //其他数据项,类型为InfoType  
    9. } RecType;              //排序的记录类型定义  
    10.   
    11. void ShellSort(RecType R[],int n)   //希尔排序算法  
    12. {  
    13.     int i,j,gap,k;  
    14.     RecType tmp;  
    15.     gap=n/2;                //增量置初值  
    16.     while (gap>0)  
    17.     {  
    18.         for (i=gap; i<n; i++) //对所有相隔gap位置的所有元素组进行排序  
    19.         {  
    20.             tmp=R[i];  
    21.             j=i-gap;  
    22.             while (j>=0 && tmp.key<R[j].key)//对相隔gap位置的元素组进行排序  
    23.             {  
    24.                 R[j+gap]=R[j];  
    25.                 j=j-gap;  
    26.             }  
    27.             R[j+gap]=tmp;  
    28.             j=j-gap;  
    29.         }  
    30.         printf("gap=%d:",gap);  
    31.         for (k=0; k<n; k++)  
    32.             printf("%d ",R[k].key);  
    33.         printf("\n");  
    34.         gap=gap/2;  //减小增量  
    35.     }  
    36. }  
    37.   
    38. int main()  
    39. {  
    40.     int i,n=11;  
    41.     RecType R[MaxSize];  
    42.     KeyType a[]= {16,25,12,30,47,11,23,36,9,18,31};  
    43.     for (i=0; i<n; i++)  
    44.         R[i].key=a[i];  
    45.     printf("排序前:");  
    46.     for (i=0; i<n; i++)  
    47.         printf("%d ",R[i].key);  
    48.     printf("\n");  
    49.     ShellSort(R,n);  
    50.     printf("排序后:");  
    51.     for (i=0; i<n; i++)  
    52.         printf("%d ",R[i].key);  
    53.     printf("\n");  
    54.     return 0;  
    55. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值