关闭

关闭

关闭

封号提示

内容

北京pk10是国家彩票吗 几种排序算法的比较

北京赛车预测号码:几种排序算法的比较.doc

几种排序算法的比较

一个高傲的人
2018-11-11 0人阅读 0 0 0 暂无简介 举报

北京pk10是国家彩票吗 www.qdpo.net 简介:本文档为《几种排序算法的比较doc》,可适用于IT/计算机领域

几种排序算法的比较#include<iostream>#include<fstream>#include<ctime>#definerandom(x)(rand()x)usingnamespacestdconstintMAXSIZE=intNinttimesboolshowSortedNumvoidCreateRandomNum()voidBubbleSort()voidInsertSort()voidBInsertSort()voidQuickSort(int*num,intLow,intHigh)voidSelectionSort()voidHeapAdjust(intarray,inti,intnLength)voidHeapSort(intarray,intlength)voidRadixSort()intmain(){charflagcout<<"请输入排序整数的个数N:"<<endlcin>>NshowSortedNum=truecout<<"是否显示排序后的整数,YN"<<endlcin>>flagif(flag=='N'){showSortedNum=false}CreateRandomNum()InsertSort()BInsertSort()BubbleSort()ifstreamoileintnumMAXSIZEintitimes=oileopen("datatxt",ios::out)for(i=i<Ni){oile>>numi}oileclose()QuickSort(num,,N)cout<<"快速排序:"<<endlcout<<"比较次数为:"<<times<<endlif(showSortedNum){for(i=i<Ni){cout<<numi<<""if(N==i){cout<<endl}}QuickSort}SelectionSort()times=oileopen("datatxt",ios::out)for(i=i<Ni){oile>>numi}oileclose()HeapSort(num,N)cout<<"堆排序:"<<endlcout<<"比较次数为:"<<times<<endlif(showSortedNum)for(i=i<Ni){cout<<numi<<""if(N==i){cout<<endl}}堆排序RadixSort()return}voidCreateRandomNum(){ofstreaminFilesrand((int)time())inFileopen("datatxt",ios::in)if(!inFileisopen()){cout<<"Couldnotopenthefilen"cout<<"Programterminatingn"exit(EXITFAILURE)}for(inti=i<Ni){inFile<<random()<<endl}inFileclose()}voidInsertSort(){intnumMAXSIZEinti,jinttempifstreamoileinttimes=oileopen("datatxt",ios::out)for(i=i<Ni){oile>>numi}oileclose()cout<<"直接插入排序:"<<endlfor(i=i<Ni){temp=numifor(j=i(j>)(temp<numj)j){numj=numjtimes}numj=temp}for(i=i<Ni){if(numi<numi){temp=numinumi=numitimesfor(j=itemp<numjj>=j){timesnumj=numj}numj=temp}}cout<<"比较次数为:"<<times<<endlif(showSortedNum)for(i=i<Ni){cout<<numi<<""if(N==i){cout<<endl}}}voidBInsertSort(){intnumMAXSIZEinti,jintlow,high,minttempifstreamoileinttimes=oileopen("datatxt",ios::out)for(i=i<Ni){oile>>numi}oileclose()cout<<"折半插入排序:"<<endlfor(i=i<Ni){temp=numilow=high=iwhile(low<=high){m=(lowhigh)if(temp<numm){timeshigh=m}else{low=m}}for(j=ij>=highj){timesnumj=numj}numhigh=temp}cout<<"比较次数为:"<<times<<endlif(showSortedNum)for(i=i<Ni){cout<<numi<<""if(N==i){cout<<endl}}}voidBubbleSort(){inti,j,tempintnumMAXSIZEifstreamoileinttimes=oileopen("datatxt",ios::out)for(i=i<Ni){oile>>numi}oileclose()cout<<"冒泡排序:"<<endlfor(i=i<Ni)for(j=ij<Nj){if(numi>numj){timestemp=numinumi=numjnumj=temp}}cout<<"比较次数为:"<<times<<endlif(showSortedNum)for(i=i<Ni){cout<<numi<<""if(N==i){cout<<endl}}}voidQuickSort(int*num,intLow,intHigh){intlow,high,templow=Lowhigh=Hightemp=numLowwhile(low<high){while((low<high)(temp<=numhigh)){timeshigh}numlow=numhighwhile((low<high)(temp>=numlow)){timeslow}numhigh=numlow}numlow=tempif(low>Low){timesQuickSort(num,Low,low)}if(high<High){timesQuickSort(num,high,High)}}voidSelectionSort(){inti,j,tempintnumMAXSIZEifstreamoileinttimes=oileopen("datatxt",ios::out)for(i=i<Ni){oile>>numi}oileclose()cout<<"选择排序:"<<endlfor(i=i<Ni)for(j=ij<Nj){if(numi>numj){timestemp=numjnumj=numinumi=temp}}cout<<"比较次数为:"<<times<<endlif(showSortedNum)for(i=i<Ni){cout<<numi<<""if(N==i){cout<<endl}}}voidHeapAdjust(intarray,inti,intnLength){intnChildintnTempfor(nTemp=arrayi*i<nLengthi=nChild){nChild=*iif(nChild<nLengtharraynChild>arraynChild){timesnChild}if(nTemp<arraynChild){timesarrayi=arraynChild}else{break}arraynChild=nTemp}}voidHeapSort(intarray,intlength){inttempintifor(i=lengthi>=i){timesHeapAdjust(array,i,length)}for(i=lengthi>i){timestemp=arrayarray=arrayiarrayi=tempHeapAdjust(array,,i)}}voidRadixSort(){intnumMAXSIZEinttempNumMAXSIZEintiifstreamoileoileopen("datatxt",ios::out)for(i=i<Ni){oile>>numi}oileclose()cout<<"基数排序:"<<endlintj,kintcountinttemintradix=for(i=i<i){for(j=j<j){countj=}for(j=j<Nj){k=(numjradix)countk}tem=for(j=j<j)temj=temjcountjfor(j=j<Nj){k=(numjradix)tempNumtemk=numjtemk}for(j=j<Nj){numj=tempNumj}radix*=}cout<<"基数排序不需要比较。。。"<<endlif(showSortedNum)for(i=i<Ni){cout<<numi<<""if(N==i){cout<<endl}}}

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

评分:

/17

意见
反馈

立即扫码关注

爱问共享资料微信公众号

北京pk10是国家彩票吗

举报
资料