实验九各种排序算法性能比较.docx
《实验九各种排序算法性能比较.docx》由会员分享,可在线阅读,更多相关《实验九各种排序算法性能比较.docx(11页珍藏版)》请在冰豆网上搜索。
![实验九各种排序算法性能比较.docx](https://file1.bdocx.com/fileroot1/2023-2/6/bac64f14-c09d-4567-a78f-d7f4ab3ebe28/bac64f14-c09d-4567-a78f-d7f4ab3ebe281.gif)
实验九各种排序算法性能比较
课程名称:
《数据结构》课程设计
课程设计题目:
9、各种排序算法性能比较
姓名:
费富增
院系:
计算机学院
专业:
计算机科学与技术
年级:
2012
学号:
E01214108
指导教师:
王爱平
2014年10月17日
目录
1课程的目的………………………………………………………………1
2需求分析………………………………………………………………………1
3课程设计报告内容……………………………………………………………1
3.1概要设计……………………………………………………………………1
3.2详细设计……………………………………………………………………2
3.3调试分析……………………………………………………………………9
3.4用户手册……………………………………………………………………9
3.5测试结果……………………………………………………………………9
3.6程序清单……………………………………………………………………9
4小结…………………………………………………………………………9
5参考文献………………………………………………………………9
1.课程设计的目的
(1)熟练使用C语言编写程序,解决实际问题;
(2)了解并掌握数据结构与算法的设计方法,具备初步的独立分析和设计能力;
(3)初步掌握软件开发过程的问题分析、系统设计、程序编码、测试等基本方法和技能;
(4)提高综合运用所学的理论知识和方法独立分析和解决问题的能力;
2.需求分析
(1)使用数组来存放产生的2000个随机数
(2)编写统计程序运行时间的函数
(3)编写快速排序、冒泡排序、插入排序、梳排序四种排序算法的函数
(4)编写主函数,控制程序运行
3文件串的加密的设计
3.1概要设计
1.函数
(1)使用四种排序算法:
插入排序、冒泡排序、快速排序、梳排序
(2)使用clock()函数来统计每种排序方法的计算时间。
流程图:
3.2详细设计
源程序:
#include
#include
#include
#defineMAX40000
voidinsertSort(inta[],intlen)//插入排序
{
inti,j,temp;
for(i=1;i{
temp=a[i];
for(j=i-1;j>=0;j--)
{
if(a[j]>temp)
{
a[j+1]=a[j];
}else
{
break;
}}
a[j+1]=temp;
}}
voidBubble(inta[],intlen)//冒泡排序
{
intlength=len;
inti=0;
intj=0;
for(i=0;i{
for(j=0;j{
if(a[j]>a[j+1])
{
inttemp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
length--;
j=0;
}
}
intpartions(intl[],intlow,inthigh)//快速排序
{
intprvotkey=l[low];
l[0]=l[low];
while(low{
while(low=prvotkey)
--high;
l[low]=l[high];
while(low++low;
l[high]=l[low];
}
l[low]=l[0];
returnlow;
}
voidqsort(intl[],intlow,inthigh)
{
intprvotloc;
if(low{
prvotloc=partions(l,low,high);
qsort(l,low,prvotloc-1);
qsort(l,prvotloc+1,high);
}
}
voidquicksort(intl[],intn)
{
qsort(l,1,n);
}
voidcombsort(inta[],intn)//梳排序
{
floatshrink_factor=1.3;
intgap=n,swapped=1,swap,i;
while((gap>1)||swapped)
{
if(gap>1)gap=gap/shrink_factor;
swapped=0;
i=0;
while((gap+i){
if(a[i]-a[i+gap]>0)
{
swap=a[i];
a[i]=a[i+gap];
a[i+gap]=swap;
swapped=1;
}
++i;
}}}
intmain()
{printf("正在进行排序,请稍等:
\n");
printf(".......\n");
intnumber[MAX]={0};
intnumber1[MAX]={0};
intnumber2[MAX]={0};
intnumber3[MAX]={0};
intnumber4[MAX]={0};
inti;
srand((unsigned)time(NULL));/*播种子*/
for(i=0;i{
number[i]=rand()%20000;/*产生101以内的随机整数*/
number1[i]=number2[i]=number3[i]=number4[i]=number[i];
while(number[i]==0)
{
number[i]=rand()%20000;
number1[i]=number2[i]=number3[i]=number4[i]=number[i];
}
}
clock_tbegin1,end1;//快速排序并计算时间
doublecost1;
begin1=clock();
quicksort(number1,MAX);
end1=clock();
cost1=(double)(end1-begin1)/CLOCKS_PER_SEC;
clock_tbegin2,end2;//冒泡排序并计算时间
doublecost2;
begin2=clock();
Bubble(number2,MAX);
end2=clock();
cost2=(double)(end2-begin2)/CLOCKS_PER_SEC;
clock_tbegin3,end3;//插入排序并计算时间
doublecost3;
begin3=clock();
insertSort(number3,MAX);
end3=clock();
cost3=(double)(end3-begin3)/CLOCKS_PER_SEC;
clock_tbegin4,end4;//梳排序并计算时间
doublecost4;
begin4=clock();
combsort(number4,MAX);
end4=clock();
cost4=(double)(end4-begin4)/CLOCKS_PER_SEC
for(intj=0;j{printf("%d",number1[j]);}
printf("\n");
printf("你好,排序完成!
\n");
printf("快速排序耗时:
%lf秒\n",cost1);
printf("冒泡排序耗时:
%lf秒\n",cost2);
printf("插入排序耗时:
%lf秒\n",cost3);
printf("梳排序耗时:
%lf秒\n",cost4);
return0;}
3.3调试分析
由于这个程序中冒泡排序运行时间较长,在一开始运行时可能与以往不一样,但这是正常的。
总体起来,该程序比较简单,他们的clock函数都大致一样。
冒泡排序、快速排序和插入排序以前学过,主要时需要编写梳排序。
该程序通过最后的四者时间比较,可以得出梳排序和快速排序较快。
3.4用户手册(略)
3.5测试结果(略)
4总结
通过这次课程设计我主要熟悉了快速排序、冒泡排序、插入排序、梳排序四种排序算法的具体实现方法。
我认识到了排序功能在解决实际问题中的作用,使我对数据结构的学习有了更进一步的理解。
在完成本次课程设计中,我发现很多理论性知识在实际使用时与单纯的理论还是有所差别的,今后我会更加注重培养自己的实践动手能力。
5、程序清单:
(见附录)
6、参考文献
1严蔚敏,吴伟民编著.数据结构(C语言版)--北京:
清华大学出版社,2007.2严蔚敏,吴伟民米宁编著.数据结构题集(C语言版)--北京:
清华大学出版社,2007.3网上搜索相关程序作为参考
7、程序运行结果
运行截图