【第16周 项目2 - 大数据集上排序算法性能的体验】

问题:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*   
  2. * Copyright (c)2015级,烟台大学计算机与控制工程学院   
  3. * All rights reserved.   
  4. * 文件名称:11.cpp   
  5. * 作    者:彭友程   
  6. * 完成日期:2016年12月16日   
  7. * 版 本 号:v1.0    
  8. *问题描述:采用归并排序、快速排序等高效算法进行排序,当数据元素较少时(如n≤64),经常直接使用直接插入排序算法等高复杂度的算法。这样做,会带来一定的好处,例如归并排序减少分配、回收临时存储区域的频次,快速排序减少递归层次等。  
  9. 试按上面的思路,重新实现归并排序算法。 
  10. 提示1:这一项目需要整合多种排序算法,可以考虑先建设排序算法库,作为我们这门课算法库的收官之作;  
  11. 提示2:本项目旨在获得对于复杂度不同算法的感性认识,由于数据分布特点、计算机运行状态等不同,其结果并不能完全代替对算法复杂度的理论分析;  
  12. 提示3:由于C语言标准提供的时间函数只精确到秒,几种O(nlog2n)级别的算法,在5万条记录的压力下,并不能明显地看出优劣,可以忽略直接插入排序、冒泡排序、直接选择排序这三种相对低效率的算法(以节约时间。若能够忍受他们长时间地运行,请自便),成10倍地加大数据量,然后进行观察。 
  13. *输入描述:无   
  14. *程序输出:测试数据   
  15. */    

sort.h代码:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>    
  2. #include <malloc.h>    
  3. #include <stdlib.h>    
  4. #include <time.h>    
  5. #define MaxSize 50000      //最多的数据,取5万,只测试快速算法,可以往大调整    
  6.     
  7. //下面的符号常量和结构体针对基数排序    
  8. #define Radix 10           //基数的取值    
  9. #define Digits 10          //关键字位数    
  10.     
  11. typedef int KeyType;    //定义关键字类型    
  12. typedef char InfoType[10];    
  13. typedef struct          //记录类型    
  14. {    
  15.     KeyType key;        //关键字项    
  16.     InfoType data;      //其他数据项,类型为InfoType    
  17. } RecType;              //排序的记录类型定义    
  18.     
  19. typedef struct node    
  20. {    
  21.     KeyType data;      //记录的关键字,同算法讲解中有差别    
  22.     struct node *next;    
  23. } RadixRecType;    
  24.     
  25. void InsertSort(RecType R[],int n); //直接插入排序    
  26. void ShellSort(RecType R[],int n);  //希尔排序算法    
  27. void BubbleSort(RecType R[],int n); //冒泡排序    
  28. void QuickSort(RecType R[],int n);  //快速排序    
  29. void SelectSort(RecType R[],int n);  //直接选择排序    
  30. void HeapSort(RecType R[],int n);  //堆排序    
  31. void MergeSort(RecType R[],int n); //归并排序    
  32.     
  33. //下面函数支持基数排序    
  34. void CreateLink(RadixRecType *&p,RecType R[],int n);   //创建基数排序用的链表    
  35. void DestoryLink(RadixRecType *&p); //释放基数排序用的链表    
  36. void RadixSort(RadixRecType *&p); //基数排序    

sort.cpp代码:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "sort.h"    
  2. #include <malloc.h>    
  3.     
  4. //1. 对R[0..n-1]按递增有序进行直接插入排序    
  5. void InsertSort(RecType R[],int n)    
  6. {    
  7.     int i,j;    
  8.     RecType tmp;    
  9.     for (i=1; i<n; i++)    
  10.     {    
  11.         tmp=R[i];    
  12.         j=i-1;            //从右向左在有序区R[0..i-1]中找R[i]的插入位置    
  13.         while (j>=0 && tmp.key<R[j].key)    
  14.         {    
  15.             R[j+1]=R[j]; //将关键字大于R[i].key的记录后移    
  16.             j--;    
  17.         }    
  18.         R[j+1]=tmp;      //在j+1处插入R[i]    
  19.     }    
  20. }    
  21.     
  22.     
  23. //2. 希尔排序算法    
  24. void ShellSort(RecType R[],int n)    
  25. {    
  26.     int i,j,gap;    
  27.     RecType tmp;    
  28.     gap=n/2;                //增量置初值    
  29.     while (gap>0)    
  30.     {    
  31.         for (i=gap; i<n; i++) //对所有相隔gap位置的所有元素组进行排序    
  32.         {    
  33.             tmp=R[i];    
  34.             j=i-gap;    
  35.             while (j>=0 && tmp.key<R[j].key)//对相隔gap位置的元素组进行排序    
  36.             {    
  37.                 R[j+gap]=R[j];    
  38.                 j=j-gap;    
  39.             }    
  40.             R[j+gap]=tmp;    
  41.             j=j-gap;    
  42.         }    
  43.         gap=gap/2;  //减小增量    
  44.     }    
  45. }    
  46.     
  47. //3. 冒泡排序    
  48. void BubbleSort(RecType R[],int n)    
  49. {    
  50.     int i,j,exchange;    
  51.     RecType tmp;    
  52.     for (i=0; i<n-1; i++)    
  53.     {    
  54.         exchange=0;    
  55.         for (j=n-1; j>i; j--)   //比较,找出最小关键字的记录    
  56.             if (R[j].key<R[j-1].key)    
  57.             {    
  58.                 tmp=R[j];  //R[j]与R[j-1]进行交换,将最小关键字记录前移    
  59.                 R[j]=R[j-1];    
  60.                 R[j-1]=tmp;    
  61.                 exchange=1;    
  62.             }    
  63.         if (exchange==0)    //没有交换,即结束算法    
  64.             return;    
  65.     }    
  66. }    
  67.     
  68. //4. 对R[s]至R[t]的元素进行快速排序    
  69. void QuickSortR(RecType R[],int s,int t)    
  70. {    
  71.     int i=s,j=t;    
  72.     RecType tmp;    
  73.     if (s<t)                //区间内至少存在两个元素的情况    
  74.     {    
  75.         tmp=R[s];           //用区间的第1个记录作为基准    
  76.         while (i!=j)        //从区间两端交替向中间扫描,直至i=j为止    
  77.         {    
  78.             while (j>i && R[j].key>=tmp.key)    
  79.                 j--;        //从右向左扫描,找第1个小于tmp.key的R[j]    
  80.             R[i]=R[j];      //找到这样的R[j],R[i]"R[j]交换    
  81.             while (i<j && R[i].key<=tmp.key)    
  82.                 i++;        //从左向右扫描,找第1个大于tmp.key的记录R[i]    
  83.             R[j]=R[i];      //找到这样的R[i],R[i]"R[j]交换    
  84.         }    
  85.         R[i]=tmp;    
  86.         QuickSortR(R,s,i-1);     //对左区间递归排序    
  87.         QuickSortR(R,i+1,t);     //对右区间递归排序    
  88.     }    
  89. }    
  90.     
  91. //4. 快速排序辅助函数,对外同其他算法统一接口,内部调用递归的快速排序    
  92. void QuickSort(RecType R[],int n)    
  93. {    
  94.     QuickSortR(R, 0, n-1);    
  95. }    
  96.     
  97. //5. 直接选择排序    
  98. void SelectSort(RecType R[],int n)    
  99. {    
  100.     int i,j,k;    
  101.     RecType temp;    
  102.     for (i=0; i<n-1; i++)           //做第i趟排序    
  103.     {    
  104.         k=i;    
  105.         for (j=i+1; j<n; j++)   //在当前无序区R[i..n-1]中选key最小的R[k]    
  106.             if (R[j].key<R[k].key)    
  107.                 k=j;            //k记下目前找到的最小关键字所在的位置    
  108.         if (k!=i)               //交换R[i]和R[k]    
  109.         {    
  110.             temp=R[i];    
  111.             R[i]=R[k];    
  112.             R[k]=temp;    
  113.         }    
  114.     }    
  115. }    
  116.     
  117. //6. 堆排序辅助之——调整堆    
  118. void sift(RecType R[],int low,int high)    
  119. {    
  120.     int i=low,j=2*i;                        //R[j]是R[i]的左孩子    
  121.     RecType temp=R[i];    
  122.     while (j<=high)    
  123.     {    
  124.         if (j<high && R[j].key<R[j+1].key)  //若右孩子较大,把j指向右孩子    
  125.             j++;                                //变为2i+1    
  126.         if (temp.key<R[j].key)    
  127.         {    
  128.             R[i]=R[j];                          //将R[j]调整到双亲结点位置上    
  129.             i=j;                                //修改i和j值,以便继续向下筛选    
  130.             j=2*i;    
  131.         }    
  132.         else break;                             //筛选结束    
  133.     }    
  134.     R[i]=temp;                                  //被筛选结点的值放入最终位置    
  135. }    
  136.     
  137. //6. 堆排序    
  138. void HeapSort(RecType R[],int n)    
  139. {    
  140.     int i;    
  141.     RecType temp;    
  142.     for (i=n/2; i>=1; i--) //循环建立初始堆    
  143.         sift(R,i,n);    
  144.     for (i=n; i>=2; i--) //进行n-1次循环,完成推排序    
  145.     {    
  146.         temp=R[1];       //将第一个元素同当前区间内R[1]对换    
  147.         R[1]=R[i];    
  148.         R[i]=temp;    
  149.         sift(R,1,i-1);   //筛选R[1]结点,得到i-1个结点的堆    
  150.     }    
  151. }    
  152.     
  153. //7.归并排序辅助1——合并有序表    
  154. void Merge(RecType R[],int low,int mid,int high)    
  155. {    
  156.     RecType *R1;    
  157.     int i=low,j=mid+1,k=0; //k是R1的下标,i、j分别为第1、2段的下标    
  158.     R1=(RecType *)malloc((high-low+1)*sizeof(RecType));  //动态分配空间    
  159.     while (i<=mid && j<=high)       //在第1段和第2段均未扫描完时循环    
  160.         if (R[i].key<=R[j].key)     //将第1段中的记录放入R1中    
  161.         {    
  162.             R1[k]=R[i];    
  163.             i++;    
  164.             k++;    
  165.         }    
  166.         else                            //将第2段中的记录放入R1中    
  167.         {    
  168.             R1[k]=R[j];    
  169.             j++;    
  170.             k++;    
  171.         }    
  172.     while (i<=mid)                      //将第1段余下部分复制到R1    
  173.     {    
  174.         R1[k]=R[i];    
  175.         i++;    
  176.         k++;    
  177.     }    
  178.     while (j<=high)                 //将第2段余下部分复制到R1    
  179.     {    
  180.         R1[k]=R[j];    
  181.         j++;    
  182.         k++;    
  183.     }    
  184.     for (k=0,i=low; i<=high; k++,i++) //将R1复制回R中    
  185.         R[i]=R1[k];    
  186. }    
  187.     
  188. //7. 归并排序辅助2——一趟归并    
  189. void MergePass(RecType R[],int length,int n)    //对整个数序进行一趟归并    
  190. {    
  191.     int i;    
  192.     for (i=0; i+2*length-1<n; i=i+2*length)     //归并length长的两相邻子表    
  193.         Merge(R,i,i+length-1,i+2*length-1);    
  194.     if (i+length-1<n)                       //余下两个子表,后者长度小于length    
  195.         Merge(R,i,i+length-1,n-1);          //归并这两个子表    
  196. }    
  197.     
  198. //7. 归并排序    
  199. void MergeSort(RecType R[],int n)           //自底向上的二路归并算法    
  200. {    
  201.     int length;    
  202.     for (length=1; length<n; length=2*length) //进行log2n趟归并    
  203.         MergePass(R,length,n);    
  204. }    
  205.     
  206. //以下基数排序,为了统一测试有改造    
  207. //8. 基数排序的辅助函数,创建基数排序用的链表    
  208. void CreateLink(RadixRecType *&p,RecType R[],int n)   //采用后插法产生链表    
  209. {    
  210.     int i;    
  211.     RadixRecType *s,*t;    
  212.     for (i=0; i<n; i++)    
  213.     {    
  214.         s=(RadixRecType *)malloc(sizeof(RadixRecType));    
  215.         s->data = R[i].key;    
  216.         if (i==0)    
  217.         {    
  218.             p=s;    
  219.             t=s;    
  220.         }    
  221.         else    
  222.         {    
  223.             t->next=s;    
  224.             t=s;    
  225.         }    
  226.     }    
  227.     t->next=NULL;    
  228. }    
  229.     
  230. //8. 基数排序的辅助函数,释放基数排序用的链表    
  231. void DestoryLink(RadixRecType *&p)    
  232. {    
  233.     RadixRecType *q;    
  234.     while(p!=NULL)    
  235.     {    
  236.         q=p->next;    
  237.         free(p);    
  238.         p=q;    
  239.     }    
  240.     return;    
  241. }    
  242.     
  243. //8. 实现基数排序:*p为待排序序列链表指针,基数R和关键字位数D已经作为符号常量定义好    
  244. void RadixSort(RadixRecType *&p)    
  245. {    
  246.     RadixRecType *head[Radix],*tail[Radix],*t; //定义各链队的首尾指针    
  247.     int i,j,k;    
  248.     unsigned int d1, d2=1;   //用于分离出第i位数字,见下面的注释    
  249.     for (i=1; i<=Digits; i++)                  //从低位到高位循环    
  250.     {    
  251.         //分离出倒数第i位数字,先通过对d1=10^i取余,得到其后i位,再通过整除d2=10^(i-1)得到第i位    
  252.         //例如,分离出倒数第1位,即个位数,先对d1=10取余,再整除d2=1    
  253.         //再例如,分离出倒数第2位,即十位数,先对d1=100取余,再整除d2=10    
  254.         //循环之前,d2已经初始化为1,在这一层循环末增加10倍    
  255.         //下面根据d2,得到d1的值    
  256.         d1=d2*10;    
  257.         for (j=0; j<Radix; j++)                 //初始化各链队首、尾指针    
  258.             head[j]=tail[j]=NULL;    
  259.         while (p!=NULL)                 //对于原链表中每个结点循环    
  260.         {    
  261.             k=(p->data%d1)/d2;           //分离出第i位数字k    
  262.             if (head[k]==NULL)          //进行分配    
  263.             {    
  264.                 head[k]=p;    
  265.                 tail[k]=p;    
  266.             }    
  267.             else    
  268.             {    
  269.                 tail[k]->next=p;    
  270.                 tail[k]=p;    
  271.             }    
  272.             p=p->next;                  //取下一个待排序的元素    
  273.         }    
  274.         p=NULL;                         //重新用p来收集所有结点    
  275.         for (j=0; j<Radix; j++)             //对于每一个链队循环    
  276.             if (head[j]!=NULL)          //进行收集    
  277.             {    
  278.                 if (p==NULL)    
  279.                 {    
  280.                     p=head[j];    
  281.                     t=tail[j];    
  282.                 }    
  283.                 else    
  284.                 {    
  285.                     t->next=head[j];    
  286.                     t=tail[j];    
  287.                 }    
  288.             }    
  289.         t->next=NULL;                   //最后一个结点的next域置NULL    
  290.         //下面更新用于分离出第i位数字的d2    
  291.         d2*=10;    
  292.     }    
  293. }    

main.cpp代码:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include <stdlib.h>  
  4. #include <time.h>  
  5. #include "sort.h"  
  6.   
  7. void GetLargeData(RecType *&R, int n)  
  8. {  
  9.     srand(time(0));  
  10.     R=(RecType*)malloc(sizeof(RecType)*n);  
  11.     for(int i=0; i<n; i++)  
  12.         R[i].key= rand();  //产生0~RAND_MAX间的数  
  13.     printf("生成了%d条记录\n", n);  
  14. }  
  15.   
  16. //调用某一排序算法完成排序,返回排序用时  
  17. long Sort(RecType *&R, int n, void f(RecType*, int))  
  18. {  
  19.     int i;  
  20.     long beginTime, endTime;  
  21.     RecType *R1=(RecType*)malloc(sizeof(RecType)*n);  
  22.     for (i=0;i<n;i++)  
  23.         R1[i]=R[i];  
  24.     beginTime = time(0);  
  25.     f(R1,n);  
  26.     endTime = time(0);  
  27.     free(R1);  
  28.     return endTime-beginTime;  
  29. }  
  30.   
  31. //调用基数排序算法完成排序,返回排序用时  
  32. long Sort1(RecType *&R, int n)  
  33. {  
  34.     long beginTime, endTime;  
  35.     RadixRecType *p;  
  36.     CreateLink(p,R,n);  
  37.     beginTime = time(0);  
  38.     RadixSort(p);  
  39.     endTime = time(0);  
  40.     DestoryLink(p);  
  41.     return endTime-beginTime;  
  42. }  
  43.   
  44. int main()  
  45. {  
  46.     RecType *R;  
  47.     int n = MaxSize;   //测试中, MaxSize取50W  
  48.     GetLargeData(R, n);  
  49.     printf("各种排序花费时间:\n");  
  50.     printf("  直接插入排序:%ld\n", Sort(R, n, InsertSort));  
  51.     printf("  希尔排序:%ld\n", Sort(R, n, ShellSort));  
  52.     printf("  冒泡排序:%ld\n", Sort(R, n, BubbleSort));  
  53.     printf("  快速排序:%ld\n", Sort(R, n, QuickSort));  
  54.     printf("  直接选择排序:%ld\n", Sort(R, n, SelectSort));  
  55.     printf("  堆排序:%ld\n", Sort(R, n, HeapSort));  
  56.     printf("  归并排序:%ld\n", Sort(R, n, MergeSort));  
  57.     printf("  基数排序:%ld\n", Sort1(R, n));  
  58.     free(R);  
  59.     return 0;  
  60. }  

运行结果:

测试运行出现Bug情况,还需时间分析,测试50000时出现程序终止现象,只出现部分数据。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值