欢迎来到天天文库
浏览记录
ID:33585918
大小:163.50 KB
页数:16页
时间:2019-02-27
《数据结构6种算法排序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、排序测试代码:#include#include#include"d_random.h"#include"d_timer.h"usingnamespacestd;//#defineNum5000;templatevoidselectionSort(Tarr[],intn){intsmallIndex;intpass,j;Ttemp;for(pass=0;pass2、1;jvoidinsertionSort(vector&v){inti,j,n=v.size();Ttemp;for(i=1;i0&3、&tempvoidexchangesort(vector&v){intvsize=v.size(),temp,pass,i;for(pass=0;passv[i+1]){temp=v[pass];v[pass]=v[i+1];v[i+1]=temp;}}voidbubbl4、esort(vector&v){inttemp,n=v.size(),i,j;for(i=1;iv[j+1]){temp=v[j];v[j]=v[j+1];v[j+1]=temp;}}}templateintpivotIndex(vector&v,intfirst,intlast){intmid,scanUp,scanDown;Tpivot,temp;if(first==last)retur5、nlast;elseif(first==last-1)returnfirst;else{mid=(last+first)/2;pivot=v[mid];v[mid]=v[first];v[first]=pivot;scanUp=first+1;scanDown=last-1;while(scanUp6、])scanDown--;if(scanUpvoidquicksort(vector&v,intfirst,intlast){intpivotLoc;Ttemp;if(last-first<=1)return;7、elseif(last-first==2){if(v[last-1]voidmerge(vector&v,intfirst,8、intmid,intlast){vectortempVector;intindexA,indexB,indexV;indexA=first;indexB=mid;while(indexA
2、1;jvoidinsertionSort(vector&v){inti,j,n=v.size();Ttemp;for(i=1;i0&
3、&tempvoidexchangesort(vector&v){intvsize=v.size(),temp,pass,i;for(pass=0;passv[i+1]){temp=v[pass];v[pass]=v[i+1];v[i+1]=temp;}}voidbubbl
4、esort(vector&v){inttemp,n=v.size(),i,j;for(i=1;iv[j+1]){temp=v[j];v[j]=v[j+1];v[j+1]=temp;}}}templateintpivotIndex(vector&v,intfirst,intlast){intmid,scanUp,scanDown;Tpivot,temp;if(first==last)retur
5、nlast;elseif(first==last-1)returnfirst;else{mid=(last+first)/2;pivot=v[mid];v[mid]=v[first];v[first]=pivot;scanUp=first+1;scanDown=last-1;while(scanUp6、])scanDown--;if(scanUpvoidquicksort(vector&v,intfirst,intlast){intpivotLoc;Ttemp;if(last-first<=1)return;7、elseif(last-first==2){if(v[last-1]voidmerge(vector&v,intfirst,8、intmid,intlast){vectortempVector;intindexA,indexB,indexV;indexA=first;indexB=mid;while(indexA
6、])scanDown--;if(scanUpvoidquicksort(vector&v,intfirst,intlast){intpivotLoc;Ttemp;if(last-first<=1)return;
7、elseif(last-first==2){if(v[last-1]voidmerge(vector&v,intfirst,
8、intmid,intlast){vectortempVector;intindexA,indexB,indexV;indexA=first;indexB=mid;while(indexA
此文档下载收益归作者所有