【第16周 项目3 - 归并排序算法的改进】

问题描述:

[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. *输入描述:无   
  11. *程序输出:测试数据   
  12. */    

实现代码:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include <stdlib.h>  
  4. #include <time.h>  
  5. #define MinLength 64        //最短分段长度  
  6. typedef int KeyType;    //定义关键字类型  
  7. typedef char InfoType[10];  
  8. typedef struct          //记录类型  
  9. {  
  10.     KeyType key;        //关键字项  
  11.     InfoType data;      //其他数据项,类型为InfoType  
  12. } RecType;              //排序的记录类型定义  
  13.   
  14. void GetData(RecType *&R, int n)  
  15. {  
  16.     srand(time(0));  
  17.     R=(RecType*)malloc(sizeof(RecType)*n);  
  18.     for(int i=0; i<n; i++)  
  19.         R[i].key= rand();  
  20.     printf("生成了%d条记录\n", n);  
  21. }  
  22.   
  23. //对R[low..high]按递增有序进行直接插入排序  
  24. void InsertSort(RecType R[],int low,int high)  
  25. {  
  26.     int i,j;  
  27.     RecType tmp;  
  28.     for (i=low; i<=high; i++)  
  29.     {  
  30.         tmp=R[i];  
  31.         j=i-1;            //从右向左在有序区R[low..i-1]中找R[i]的插入位置  
  32.         while (j>=low && tmp.key<R[j].key)  
  33.         {  
  34.             R[j+1]=R[j]; //将关键字大于R[i].key的记录后移  
  35.             j--;  
  36.         }  
  37.         R[j+1]=tmp;      //在j+1处插入R[i]  
  38.     }  
  39. }  
  40.   
  41. //合并两个有序表  
  42. void Merge(RecType R[],int low,int mid,int high)  
  43. {  
  44.     RecType *R1;  
  45.     int i,j,k;  
  46.     i=low,j=mid+1,k=0; //k是R1的下标,i、j分别为第1、2段的下标  
  47.     R1=(RecType *)malloc((high-low+1)*sizeof(RecType));  //动态分配空间  
  48.     while (i<=mid && j<=high)       //在第1段和第2段均未扫描完时循环  
  49.         if (R[i].key<=R[j].key)     //将第1段中的记录放入R1中  
  50.         {  
  51.             R1[k]=R[i];  
  52.             i++;  
  53.             k++;  
  54.         }  
  55.         else                            //将第2段中的记录放入R1中  
  56.         {  
  57.             R1[k]=R[j];  
  58.             j++;  
  59.             k++;  
  60.         }  
  61.     while (i<=mid)                      //将第1段余下部分复制到R1  
  62.     {  
  63.         R1[k]=R[i];  
  64.         i++;  
  65.         k++;  
  66.     }  
  67.     while (j<=high)                 //将第2段余下部分复制到R1  
  68.     {  
  69.         R1[k]=R[j];  
  70.         j++;  
  71.         k++;  
  72.     }  
  73.     for (k=0,i=low; i<=high; k++,i++) //将R1复制回R中  
  74.         R[i]=R1[k];  
  75. }  
  76.   
  77. //一趟合并  
  78. void MergePass(RecType R[],int length,int n)    //对整个数序进行一趟归并  
  79. {  
  80.     int i;  
  81.     for (i=0; i+2*length-1<n; i=i+2*length)     //归并length长的两相邻子表  
  82.         Merge(R,i,i+length-1,i+2*length-1);  
  83.     if (i+length-1<n)                       //余下两个子表,后者长度小于length  
  84.         Merge(R,i,i+length-1,n-1);          //归并这两个子表  
  85. }  
  86.   
  87. //自底向上的二路归并算法,但太短的分段,用直接插入完成  
  88. void MergeSort(RecType R[],int n)  
  89. {  
  90.     int length, i;  
  91.     for(i=0;i<n;i+=MinLength)   //先按最短分段,用插入排序使之分段有序  
  92.         InsertSort(R, i, ((i+MinLength-1<n)?(i+MinLength-1):n));  
  93.     for (length=MinLength; length<n; length=2*length) //进行归并  
  94.     {  
  95.         MergePass(R,length,n);  
  96.     }  
  97. }  
  98. int main()  
  99. {  
  100.     int i,n=10000;  
  101.     RecType *R;  
  102.     GetData(R, n);  
  103.     MergeSort(R,n);  
  104.     printf("排序后(前300个):\n");  
  105.     i=0;  
  106.     while(i<300)  
  107.     {  
  108.         printf("%12d ",R[i].key);  
  109.         i++;  
  110.         if(i%5==0)  
  111.             printf("\n");  
  112.     }  
  113.     printf("\n");  
  114.     printf("排序后(后300个):\n");  
  115.     i=0;  
  116.     while(i<300)  
  117.     {  
  118.         printf("%12d ",R[n-300+i].key);  
  119.         i++;  
  120.         if(i%5==0)  
  121.             printf("\n");  
  122.     }  
  123.     printf("\n");  
  124.     free(R);  
  125.     return 0;  
  126. }  

运行结果:


猜你喜欢

转载自blog.csdn.net/percy0618/article/details/53689979