各种内排序算法的实现及性能的比较.docx_第1页
各种内排序算法的实现及性能的比较.docx_第2页
各种内排序算法的实现及性能的比较.docx_第3页
各种内排序算法的实现及性能的比较.docx_第4页
各种内排序算法的实现及性能的比较.docx_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实 验 报 告(2015/2016学年 第2学期)课程名称数据结构A实验名称各种内排序算法的实现及性能的比较实验时间2016年6月20日指导单位计算机科学与技术系指导教师骆健学生姓名班级学号学院(系) 管理学院专 业信息管理与信息系统一、 问题陈述(1) 验证教材的各种内排序算法(2) 分析各种内排序算法的时间复杂度(3) 改进教材中的快速排序法,使得当子集和小于10个元素时改用直接插入排序(4) 使用随机数发生器产生大数据集合,运行上述 各排序算法,使系统时钟测量个算法所需的实际时间,并进行比较。系统时钟包含在头文件“time.h”中。二、概要设计Main.cpp调用Menu.h ,为主程序。Menu.h主菜单Selectsort.h简单选择排序Insertsort.h直接插入排序Bubblesort.h冒泡排序Quicksort.h快速排序Mergesort.h合并排序三、详细设计简单选择排序:将初始序列(A0An-1)作为待排序序列,第一趟在待排序序列(A0An-1)中找最小元素,与该序列中第一个元素A0交换,这样子序列(A0)有序,下一趟排序在带牌子序列(A1An-1)中进行。第i趟排序子序列(Ai-1An-1)中进行。经过n-1趟排序后使得初始序列有序。程序流程图:开始i=0in-1Small=ij=i+1jnAjAsmallSmall=jSwap(Ai,Asmall)j+结束NNNYYi+直接插入排序:一个元素插入到有序表中,首先将其存入临时变量temp,然后从后往前查找插入位置。当temp小于表中元素时,就将该元素后移一个位置,继续比较、移动,直到temp大于等于表中元素或者到了有序表的第一个元素结束,这时就可将temp存入刚后移的元素的位置了。程序流程图:开始i0&temp0last=0;j=0jiAj+1=j时将j与第一个元素交换。程序流程图:开始leftrighti=left;j=righti+AiAleftijSwap(Ai,Aj)ijSwap(Aleft,Aj)Qsort(A,left,j-1)Qsort(A,j+1,right)结束NYYNYNNYYN两路合并排序:将有n 个元素的序列看成是n个长度为1的有序子序列,然后两两合并子序列,得到n/2个长度为2或1的有序子序列,再两两合并直到得到一个长度为n的有序序列时结束。程序流程图:开始int size=1Sizeni1=1i1+sizen-1j2=n-1Merge(A,i1,j1,i2,j2)i1=j2+1size*=2结束j2=i2+size-1NYYNNY四、程序代码selectsort.h#include /简单选择排序template void SelectSort(T A, int n) int small;for (int i=0; in-1; i+) small=i;for(int j=i+1;jn;j+)if(AjAsmall)small=j;Swap(Ai,Asmall);Insertsort.h#include /直接插入排序template void InsertSort(T A, int n) for(int i=1; i0&tempAj-1)Aj=Aj-1;j-;Aj=temp;/*ok!*/Bubblesort.h#include template void BubbleSort(T A, int n) int i,j,last; i=n-1; while(i0) last=0; for(j=0;ji;j+) if(Aj+1Aj) Swap(Aj,Aj+1); last=j; i=last; Quicksort.h#include /改进的快速排序templatevoid quick(T A,int n)int *a; int top=0,right,left,j; a=new intn;if(a=NULL) return;atop+=0;atop+=n-1; /lcfor(j=0;aj!=NULL;j+) left=aj+;right=aj; if(leftright)Swap(left,right); if(right-left15)InsertSortExt(A,left,right); elseatop+=left; atop+=QuickSort(A,left,right)-1;atop+=atop-2+2;atop+=right; templateint QuickSort(T A,int left,int right) int i,j;if(leftright)i=left;j=right+1;dodo i+;while(AiAleft);if(ij)Swap(Ai,Aj);while(ij);Swap(Aleft,Aj);return j;return 0;templatevoid InsertSortExt(T A,int left,int right) for(int i=left+1; i0 & tempAj-1) Aj=Aj-1; j-; Aj=temp; Mergesort.h#include /两路合并的C+程序template void Merge(T A,int i1,int j1,int i2,int j2) T *Temp=new Tj2-i1+1; int i=i1,j=i2,k=0; while(i=j1&j=j2)if(Ai=Aj)Tempk+=Ai+;else Tempk+=Aj+;while(i=j1)Tempk+=Ai+;while(j=j2)Tempk+=Aj+;for(i=0;ik;i+)Ai1+=Tempi;delete Temp; /合并排序的C+程序template void MergeSort(T A, int n)int i1,j1,i2,j2; int size=1; while(sizen)i1=0;while(i1+sizen-1)j2=n-1;else j2=i2+size-1;Merge(A,i1,j1,i2,j2);i1=j2+1;size*=2;Meau.h#include #include #include #include #include selectsort.h#include insertsort.h#include bubblesort.h#include quicksort.h#include mergesort.h#define SIZE 400#define TIMES 1000template class Menupublic:void printmenu();void selectsort();/简单选择排序void insertSort();/直接插入排序void bubbleSort();/冒泡排序void quickSort();/快速排序void mergeSort();/两路合并排序void childmenu();/子菜单1void childmenu2();/子菜单2void switcha();private:int a,b,c;template void Menu:printmenu()cout-内排序测试系统-endl;cout endlendlendl;cout1.简单选择排序endl;cout2.直接插入排序endl;cout3.冒泡排序endl;cout4.快速排序endl;cout5.两路合并排序endl;cout6.退出switcha();template void Menu:childmenu()cout-endl;cout1.最好情况endl;cout2.最坏情况endl;cout3.平均情况endl;cout4.返回主菜单b;if(b=4)this-printmenu();templatevoid Menu:childmenu2()cout-endl;cout1.原始算法endl;cout2.改进算法endl;cout3.返回主菜单c;if(c=3)this-printmenu();template void Menu:switcha()/coutoka;switch(a)case 1:this-selectsort();break;/okcase 2:this-insertSort();break;/okcase 3:this-bubbleSort();break;/okcase 4:this-quickSort();break;/okcase 5:this-mergeSort();break;/okcase 6:exit(1);break;default:couterrorprintmenu();break;template void printout(T A,int n)/打印数组,测试时用for(int i=0;in;i+)coutAi ;coutendl;template T *producedate(int x)/产生顺序,逆序,随机的数组int i;T *A=new TSIZE;switch(x)case 1:for(i=0;i0;i-)Ai-1=SIZE-i;return A;/逆序break;case 3:srand(time(NULL);for(i=0;iSIZE;i+)Ai=rand()%1000+1;return A;/随机break;default:couterrorendl;return A;break;template void Swap(T &a,T &b)/交换2个元素T temp=a;a=b;b=temp;template void Menu:bubbleSort()cout冒泡排序childmenu();T *A;double duration;clock_t start,finish;start=clock();coutokendl;for(int i=0;iTIMES;i+)A=producedate(b);BubbleSort(A,SIZE);delete A;finish=clock();duration=(double)(finish-start)/CLOCKS_PER_SEC;/printout(A,SIZE);cout用时: durationbubbleSort();/*ok*/template void Menu:insertSort()cout直接插入排序childmenu();T *A;double duration;/A=producedate(b);/if(A=NULL)coutinsertSort();/printout(A,SIZE);clock_t start,finish;start=clock();coutokendl;for(int i=0;iTIMES;i+)A=producedate(b);InsertSort(A,SIZE);delete A;finish=clock();duration=(double)(finish-start)/CLOCKS_PER_SEC;/printout(A,SIZE);cout用时: durationinsertSort();template void Menu:mergeSort()/this-childmenu();cout合并排序endl;cout直接用随机数据测试endl;T *A;double duration;clock_t start,finish;start=clock();coutokendl;for(int i=0;iTIMES;i+)A=producedate(3);MergeSort(A,SIZE);delete A;finish=clock();duration=(double)(finish-start)/CLOCKS_PER_SEC;/printout(A,SIZE);cout用时: durationprintmenu();/*ok*/templatevoid Menu:quickSort()this-childmenu2();T *A;double duration;clock_t start,finish;if(c=1)cout原始快速排序endl;cout直接用随机数据测试endl;start=clock();coutokendl;for(int i=0;iTIMES;i+)A=producedate(3);quick(A,SIZE);delete A;finish=clock();duration=(double)(finish-start)/CLOCKS_PER_SEC;cout用时: durationquickSort();else if(c=2)cout改进的快速排序endl;cout直接用随机数据测试endl;/*A=producedate(3);printout(A,SIZE);quick(A,SIZE);printout(A,SIZE);delete A;this-printmenu();*/T *A;start=clock();coutokendl;for(int i=0;iTIMES;i+)A=producedate(3);quick(A,SIZE);delete A;finish=clock();dura

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论