版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、算法导论第二次上机报告班级:1403018 姓名:张可心 学号一)题目一一、问题This project requires you to implement an optimized version of quicksort, and compare the performances of the following combinations:(1) Cutoff values from 0 to 50;(2) Take pivot to be the 1st element, random, median of random three, and median of
2、 random five.The tests must be done on the following three kinds of inputs:(1) sorted input;(2) reverse-ordered input;(3) random input.The size of input can be taken from 1000 to 10000. The run times must be plotted with respect to the sizes to illustrate the difference. (figure out using excel, mat
3、lab in the Report)二、问题分析实现快排的最优版本,分别选取第一个数,随机数,三个随机数的中位数,五个随机数的中位数为基数,设置顺序,随机,逆序三种形式,用_qsort(int a,int p,int r,int number)和Partition(int a,int p,int r,int number)俩函数进行排序。三、算法伪代码void _qsort(a, p, r, number)if p<r q=Partition(a,p,r,number)_qsort(a,p,q-1,number)_qsort(a,q+1,r,number)int Partition(in
4、t a,int p,int r,int number)int x,temp;if(number=1) temp=p;else if(number=2) temp=rand()%(r-p+1)+p;else if(number=3)int b5;b1=rand()%(r-p+1)+p;b2=rand()%(r-p+1)+p;b3=rand()%(r-p+1)+p;sort(b+1,b+4);temp=b2; else if(number=4)int b10;b1=rand()%(r-p+1)+p;b2=rand()%(r-p+1)+p;b3=rand()%(r-p+1)+p;b4=rand()%
5、(r-p+1)+p;b5=rand()%(r-p+1)+p;sort(b+1,b+6);temp=b3;elsetemp=p;x=atemp;swap(atemp,ap);int i=r+1;for(int j=r;j>p;j-)if(aj>=x)i-;swap(ai,aj); swap(ai-1,ap);return i-1;int main() int n,a200000; cout<<"测试数据:"cin>>n;for(int i=1;i<=n;i+) ai=random(51); cout<<"序列的顺
6、序(1顺序;2随机;3逆序):"int s;cin>>s;if(s=3) sort(a+1,a+1+n,greater<int>();/对生成数进行排列 else if(s=1) sort(a+1,a+1+n); else if(s=2) else cout<<"确定基数:1,第一个数;2,随机数;3,3个随机数的中位数;4,5个随机数的中位数:" int number; cin>>number; clock_t start,end;/计时 start=clock();_qsort(a,1,n,number);end
7、=clock();cout<<"用时:"<<end-start<<"ms"<<endl;for(int i=1;i<=n;i+)cout<<ai<<" "return 0; 四、算法分析对数据进行快排,通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。 五、测试结果1以第一个数为基数的排序时间,单位ms.2以随
8、机数为基数的排序时间,单位ms3以三个随机数的中位数为基数的排序时间,单位ms4以五个随机数的中位数为基数的排序时间,单位ms(二)题目二一、问题Implement Hoares algorithm and compare it with our algorithm in the textbook. (体会有重复数据情况下,算法之间的优劣)。The input is also taken form 1000 and 10000 (between 0 and 50), and the tests should be done on the random input. The run times
9、must be plotted with respect to the sizes to illustrate the difference. (figure out using excel, matlab in the Report)二、问题分析实现Hoare快排算法,并和我们的快排算法相比较,即体会重复数据情况下,算法的优劣。三、算法伪代码void _qsort(a, p, r)if p<rq=Hoare_Partition(a,p,r);_qsort(a,p,q-1);_qsort(a,q+1,r); Hoare_Partition(a,p, r) x=ap i=p-1 j=r+1
10、while(1) while(1) j=j-1 if aj=x break while(1) i=i+1 if ai>=x break ifi<j swap(ai,aj) else return j main()int n,a200000cout<<"输入数据多少:"cin>>nfor int i=1 i<=n i=i+1 ai=random(51)clock_t start ,endstart=clock() _qsort(a,1,n)end=clock()cout<<"运行时间:"<<
11、end-start<<"ms"<<endlfor inti=1 i<=n i=i+1cout<<ai<<" "return 0四、算法分析相较原本算法,对Partition部分进行改变,原算法中,主元是与它所划分的两个分区分离的,而Hoare_Partition中主元是存在于两分区中的。五、测试结果(三)题目三一、问题Implement quicksort algorithm using tail recursion and compare it with the original quicksort
12、 algorithm.The input is also taken form 20000 and 100000 (between 0 and 50), and the tests should be done on the random input. The run times must be plotted with respect to the sizes to illustrate the difference. (figure out using excel, matlab in the Report)二、问题分析用尾递归实现快排,并和普通快排相比较,输入规模在1000-10000,
13、用运行时间和规模建立关系来显示不同。三、算法伪代码void Tail_qsort(a, p, r)while p<r q=Partition(a,p,r)Tail_qsort(a,p,q-1)p=q+1Partition( a, p, r) x=ap i=r+1for int j=r j>p j=j-1if aj>=x i=i-1swap(ai,aj)swap(ai-1,ap)return i-1main()int n,a200000cout<<"输入数据多少:"cin>>nfor(int i=1;i<=n;i+) ai=random(51)clock_t start ,endstart=clock() Tai
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【四下语文】四年级下册语文《开学必背课后生字表(含生字拼音组词等)》
- (2026年)医疗质量工作计划
- 排水沟施工方案
- 小区停电应急处置方案
- 医院感染预防与控制知识考试题及答案
- 建筑工程塔吊安装拆卸手册 (标准版)
- 公墓文物墓葬保护管理规范手册
- 《PLC 控制系统升级改造手册》
- 2025-2026学年便笺盒教学设计
- 2.1第 3 课时 研究土壤小动物类群的丰富度教学设计-2023-2024学年高二上学期生物人教版(2019)选择性必修2
- 2026年7中初二物理试卷及答案
- 2026SCCM指南成人急性呼吸窘迫综合征神经肌肉阻滞剂的应用课件
- 2026年高考(广东卷)英语试题及答案
- 超声科质量控制与报告规范
- 七年级下册《道德与法治》期末质量分析
- 2026年网络安全从入门到精通网络安全知识题库与答案解析
- 湖南省长沙市开福区2024-2025学年三年级上册期末学业质量测试数学试卷(含答案)
- 公对公账户协议书
- 卖卡丁车协议书
- GB/T 23446-2025喷涂聚脲防水涂料
- GB/T 18324-2025滑动轴承铜合金轴套尺寸和公差
评论
0/150
提交评论