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

上传人:b****8 文档编号:11458854 上传时间:2023-03-01 格式:DOCX 页数:11 大小:626.98KB
下载 相关 举报
中国科技大学算法导论第一次实验报告.docx_第1页
第1页 / 共11页
中国科技大学算法导论第一次实验报告.docx_第2页
第2页 / 共11页
中国科技大学算法导论第一次实验报告.docx_第3页
第3页 / 共11页
中国科技大学算法导论第一次实验报告.docx_第4页
第4页 / 共11页
中国科技大学算法导论第一次实验报告.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

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

《中国科技大学算法导论第一次实验报告.docx》由会员分享,可在线阅读,更多相关《中国科技大学算法导论第一次实验报告.docx(11页珍藏版)》请在冰豆网上搜索。

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

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

快速排序实验报告

SA14225010

 

1、题目

当输入数据已经“几乎”有序时,插入排序速度很快。

在实际应用中,我们可以利用这一特点来提高快速排序的速度。

当对一个长度小于k的子数组调用快速排序时,让它不做任何排序就返回。

当上层的快速排序调用返回后,对整个数组运行插入排序来完成排序过程。

试证明:

这一排序算法的期望时间复杂度为O(nk+nlg(n/k))。

分别从理论和实践的角度说明我们应该如何选择k?

2、算法思想

当输入数据已经“几乎”有序时,插入排序速度很快。

当对一个长度小于k的子数组调用快速排序时,让它不做任何排序就返回。

当上层的快速排序调用返回后,对整个数组运行插入排序来完成排序过程。

累加k的值,计算出当k为不同值时算法运行的时间,来算出当k大约为什么值时运行的时间最短,并与传统的快速排序算法的运行时间进行比较。

3、实验结果

输入100个不同的整数值,选取不同的k的值,观察所用时间

 

 

 

 

 

 

 

 

4、实验分析

理论上看,k的值选取为20到25较好;但是,从实际上来看,当k为50左右时间为39毫秒,最少,但不同的时刻运行后的时间都不相同,而且不同的输入时刻的运行时间也不相同,当数据较大时候,对k的值的选取有会有所不同,同时不同性能的机器对测试结果也不同,所以对于k值的选取没有固定的数值。

 

#include

#include

usingnamespacestd;

#defineM40

voidswap(int*a,int*b)

{

inttem;

tem=*a;

*a=*b;

*b=tem;

}

 

intpartition(intv[],constintlow,constinthigh)

{

inti,pivotpos,pivot;

pivotpos=low;

pivot=v[low];

for(i=low+1;i<=high;++i)

{

if(v[i]

{

pivotpos++;

if(pivotpos!

=i)swap(v[i],v[pivotpos]);

}

}

v[low]=v[pivotpos];

v[pivotpos]=pivot;

//cout<<"thepartitionfunctioniscalled\n";

returnpivotpos;

}

 

/*

voidQuickSort(inta[],constintlow,constinthigh)

{

intitem;

if(low

{

item=partition(a,low,high);

QuickSort(a,low,item-1);

QuickSort(a,item+1,high);

}

}

*/

 

voidQuickSort(inta[],constintlow,constinthigh)

{

intitem;

if(high-low<=M)return;

if(low

{

item=partition(a,low,high);

QuickSort(a,low,item-1);

QuickSort(a,item+1,high);

}

//cout<<"theQuickSortiscalled"<

}

voidInsertSort(inta[],constintlow,constinthigh)

{

inti,j;

inttem;

for(i=1;i

{

tem=a[i];

j=i-1;

while(j>=0&&tem

{

a[j+1]=a[j];

j--;

}

a[j+1]=tem;

}

//cout<<"theInsertSortiscalled"<

}

 

voidHybridSort(inta[],constintlow,constinthigh)

{

QuickSort(a,low,high);

InsertSort(a,low,high);

cout<<"theHybidSortiscalled"<

}

 

intmain()

{

inti,a[100];

//int*a=NULL;

longintt;

structtimebt1,t2;

/*cout<<"pleaseinputthenumberoftheelement:

"<

cin>>n;

a=(int*)malloc(n*sizeof(int));

cout<<"pleaseinputeveryelement:

"<

*/

for(i=0;i<100;i++)

{

a[i]=i+10;

}

//QuickSort(a,0,n-1);

ftime(&t1);

HybridSort(a,0,99);

cout<<"aftersortedquickly,theresultis"<

for(i=0;i<100;i++)

{

cout<

if(i%10==0)cout<

}

cout<

ftime(&t2);

t=(t2.time-t1.time)*1000+(t2.millitm-t1.millitm);/*计算时间差*/

printf("k=%d用时%ld毫秒\n",M,t);

//cout<<"thememoryofarrayaisfree"<

//free(a);

cout<<"\n"<

 

return0;

}

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1