欢迎来到天天文库
浏览记录
ID:50616053
大小:323.00 KB
页数:10页
时间:2020-03-07
《C语言四种排序算法时间复杂度比较.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1、方案设计:我这次实验通过随机生成30000个随机数,把随机数存到数组中,用这同一组随机数据分别进行四种排序,直接插入排序、直接选择排序、冒泡排序和快速排序。还通过了调用txt文件把运算所需时间导出,分别输出各个算法所需用时并对用时时长再进行冒泡排序算出用时最短的算法。2、程序代码:#include#include#include#include#include#defineN30000voidWrong()//输入错误{printf("语法错误,请重新输入!
2、");getchar();}voidDisp(inta[])//清屏{inti;system("cls");for(i=0;i0&&a[j-1]>temp;j--)a[j]=a[j-1];a[j]=temp;}}voidSelectSort(inta[],intp)//
3、选择排序算法{inti,j,k;for(i=0;ii;j--)//比较,找出本趟最小关键字的记录if(a[j]4、[j-1]=temp;}}}voidquicksort(inta[],intn,intp)//快速排序算法{inti,j,low,high,temp,top=-1;structnode{intlow,high;}st[N];top++;st[top].low=0;st[top].high=n-1;while(top>-1){low=st[top].low;high=st[top].high;top--;i=low;j=high;if(lowtemp)j--;if(i5、6、formanceFrequency(&m_liPerfFreq);LARGE_INTEGERm_liPerfStart={0};QueryPerformanceCounter(&m_liPerfStart);InsertSort(b,p);LARGE_INTEGERliPerfNow={0};QueryPerformanceCounter(&liPerfNow);doubletime=liPerfNow.QuadPart-m_liPerfStart.QuadPart;time/=m_liPerfFreq.QuadPart;if(p!=6){Disp(b);g7、etchar();}printf("用直接插入排序法用的时间为%f秒;",time);FILE*fp;fp=fopen("直接插入排序.txt","w");for(i=0;i8、liPerfFreq);LARGE_INTEGERm
4、[j-1]=temp;}}}voidquicksort(inta[],intn,intp)//快速排序算法{inti,j,low,high,temp,top=-1;structnode{intlow,high;}st[N];top++;st[top].low=0;st[top].high=n-1;while(top>-1){low=st[top].low;high=st[top].high;top--;i=low;j=high;if(lowtemp)j--;if(i
5、6、formanceFrequency(&m_liPerfFreq);LARGE_INTEGERm_liPerfStart={0};QueryPerformanceCounter(&m_liPerfStart);InsertSort(b,p);LARGE_INTEGERliPerfNow={0};QueryPerformanceCounter(&liPerfNow);doubletime=liPerfNow.QuadPart-m_liPerfStart.QuadPart;time/=m_liPerfFreq.QuadPart;if(p!=6){Disp(b);g7、etchar();}printf("用直接插入排序法用的时间为%f秒;",time);FILE*fp;fp=fopen("直接插入排序.txt","w");for(i=0;i8、liPerfFreq);LARGE_INTEGERm
6、formanceFrequency(&m_liPerfFreq);LARGE_INTEGERm_liPerfStart={0};QueryPerformanceCounter(&m_liPerfStart);InsertSort(b,p);LARGE_INTEGERliPerfNow={0};QueryPerformanceCounter(&liPerfNow);doubletime=liPerfNow.QuadPart-m_liPerfStart.QuadPart;time/=m_liPerfFreq.QuadPart;if(p!=6){Disp(b);g
7、etchar();}printf("用直接插入排序法用的时间为%f秒;",time);FILE*fp;fp=fopen("直接插入排序.txt","w");for(i=0;i8、liPerfFreq);LARGE_INTEGERm
8、liPerfFreq);LARGE_INTEGERm
此文档下载收益归作者所有