第十五周项目1-(3)-验证希尔排序算法

来源:互联网 发布:linux重置路由命令 编辑:程序博客网 时间:2024/05/18 03:01

问题及代码:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*    
  2. * Copyright (c)2016,烟台大学计算机与控制工程学院    
  3. * All rights reserved.    
  4. * 文件名称:ccc.cpp    
  5. * 作    者:陈晓琳 
  6. * 完成日期:2016年12月16日    
  7. * 版 本 号:v1.0     
  8. *问题描述:验证希尔排序,完成测试。     
  9. *输入描述:无    
  10. *程序输出:测试数据    
  11. */        

希尔排序算法:

[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;    
  51. }    

运行结果:


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

[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
原创粉丝点击