中国科技大学算法导论_第一次实验报告.doc

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
中国科技大学算法导论_第一次实验报告

快速排序实验报告 S 题目 当输入数据已经“几乎”有序时,插入排序速度很快。在实际应用中,我们可以利用这一特点来提高快速排序的速度。当对一个长度小于k的子数组调用快速排序时,让它不做任何排序就返回。当上层的快速排序调用返回后,对整个数组运行插入排序来完成排序过程。试证明:这一排序算法的期望时间复杂度为O(nk+nlg(n/k))。分别从理论和实践的角度说明我们应该如何选择k? 算法思想 当输入数据已经“几乎”有序时,插入排序速度很快。当对一个长度小于k的子数组调用快速排序时,让它不做任何排序就返回。当上层的快速排序调用返回后,对整个数组运行插入排序来完成排序过程。累加k的值,计算出当k为不同值时算法运行的时间,来算出当k大约为什么值时运行的时间最短,并与传统的快速排序算法的运行时间进行比较。 实验结果 输入100个不同的整数值,选取不同的k的值,观察所用时间 实验分析 理论上看,k的值选取为20到25较好;但是,从实际上来看,当k为50左右时间为39毫秒,最少,但不同的时刻运行后的时间都不相同,而且不同的输入时刻的运行时间也不相同,当数据较大时候,对k 的值的选取有会有所不同,同时不同性能的机器对测试结果也不同,所以对于k值的选取没有固定的数值。 #includeiostream #includesys/timeb.h using namespace std; #define M 40 void swap(int * a,int * b) { int tem; tem=*a; *a=*b; *b=tem; } int partition(int v[],const int low,const int high) { int i,pivotpos,pivot; pivotpos=low; pivot=v[low]; for(i=low+1;i=high;++i) { if(v[i]pivot) { pivotpos++; if(pivotpos!=i)swap(v[i],v[pivotpos]); } } v[low]=v[pivotpos]; v[pivotpos]=pivot; //coutthe partition function is called\n; return pivotpos; } /* void QuickSort(int a[], const int low,const int high) { int item; if(lowhigh) { item=partition(a,low,high); QuickSort(a,low,item-1); QuickSort(a,item+1,high); } } */ void QuickSort(int a[], const int low,const int high) { int item; if(high-low=M)return; if(lowhigh) { item=partition(a,low,high); QuickSort(a,low,item-1); QuickSort(a,item+1,high); } // coutthe QuickSort is calledendl; } void InsertSort(int a[],const int low,const int high) { int i,j; int tem; for(i=1;ihigh+1;++i) { tem=a[i]; j=i-1; while(j=0tema[j]) { a[j+1]=a[j]; j--; } a[j+1]=tem; } //coutthe InsertSort is calledendl; } void HybridSort(int a[],const int low,const int high) { QuickSort(a,low,high); InsertSort(a,low,high); coutthe HybidSort is calledendl; } int main() { int i,a[100]; //int *a=NULL; long int t; struct timeb t1,t2; /*coutplease input the number of the element:endl;

文档评论(0)

dajuhyy + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档