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

上传人:F****n 文档编号:98150429 上传时间:2019-09-08 格式:DOC 页数:10 大小:1.02MB
返回 下载 相关 举报
中国科技大学算法导论-第一次实验报告_第1页
第1页 / 共10页
中国科技大学算法导论-第一次实验报告_第2页
第2页 / 共10页
中国科技大学算法导论-第一次实验报告_第3页
第3页 / 共10页
中国科技大学算法导论-第一次实验报告_第4页
第4页 / 共10页
中国科技大学算法导论-第一次实验报告_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《中国科技大学算法导论-第一次实验报告》由会员分享,可在线阅读,更多相关《中国科技大学算法导论-第一次实验报告(10页珍藏版)》请在金锄头文库上搜索。

1、快速排序实验报告SA 1、 题目当输入数据已经“几乎”有序时,插入排序速度很快。在实际应用中,我们可以利用这一特点来提高快速排序的速度。当对一个长度小于k的子数组调用快速排序时,让它不做任何排序就返回。当上层的快速排序调用返回后,对整个数组运行插入排序来完成排序过程。试证明:这一排序算法的期望时间复杂度为O(nk+nlg(n/k)。分别从理论和实践的角度说明我们应该如何选择k?2、 算法思想当输入数据已经“几乎”有序时,插入排序速度很快。当对一个长度小于k的子数组调用快速排序时,让它不做任何排序就返回。当上层的快速排序调用返回后,对整个数组运行插入排序来完成排序过程。累加k的值,计算出当k为不

2、同值时算法运行的时间,来算出当k大约为什么值时运行的时间最短,并与传统的快速排序算法的运行时间进行比较。3、 实验结果输入100个不同的整数值,选取不同的k的值,观察所用时间4、 实验分析理论上看,k的值选取为20到25较好;但是,从实际上来看,当k为50左右时间为39毫秒,最少,但不同的时刻运行后的时间都不相同,而且不同的输入时刻的运行时间也不相同,当数据较大时候,对k 的值的选取有会有所不同,同时不同性能的机器对测试结果也不同,所以对于k值的选取没有固定的数值。#include#includeusing namespace std;#define M 40void swap(int * a

3、,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=vlow;for(i=low+1;i=high;+i) if(vipivot) pivotpos+; if(pivotpos!=i)swap(vi,vpivotpos); vlow=vpivotpos;vpivotpos=pivot;/coutthe partition function is calledn;return pivotpo

4、s;/*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

5、-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;i=0&temaj) aj+1=aj; j-; aj+1=tem;/coutthe InsertSort is calledendl;void HybridSort(int a,const int low,const int high) QuickSort(a,low,high); InsertSort(a,low,h

6、igh); coutthe HybidSort is calledendl;int main() int i,a100;/int *a=NULL;long int t;struct timeb t1,t2;/*coutplease input the number of the element:n;a = (int*)malloc(n*sizeof(int);coutplease input every element:endl;*/for( i=0; i100; i+)ai=i+10;/QuickSort(a,0,n-1);ftime(&t1);HybridSort(a,0,99);cout

7、 after sorted quickly,the result isendl;for(i=0; i100; i+)coutai ;if(i%10=0)coutendl;coutendl;ftime(&t2);t=(t2.time-t1.time)*1000+(t2.millitm-t1.millitm); /* 计算时间差 */ printf(k=%d 用时%ld毫秒n,M,t);/coutthe memory of array a is freeendl;/free(a);coutnendl;return 0;氨氧化催化剂往往亦可用作醛类氧化催化剂,其原因是由于这两类反应通过类似的历程,形成相同的氧化中间物之故。上列反应中以丙烯氨氧化合成丙烯腈最为重要,下面即以此反应为例进行讨论。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号