欢迎来到天天文库
浏览记录
ID:23505568
大小:163.51 KB
页数:16页
时间:2018-11-08
《数据结构种算法排序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、排序测试代码:#include#include#include"d_random.h"#include"d_timer.h"usingnamespacestd;//#defineNum5000;templatevoidselectionSort(Tarr[],intn){intsmallIndex;intpass,j;Ttemp;for(pass=0;pass2、llIndex=j;if(smallIndex!=pass){temp=arr[pass];arr[pass]=arr[smallIndex];arr[smallIndex]=temp;}}}templatevoidinsertionSort(vector&v){inti,j,n=v.size();Ttemp;for(i=1;i0&&tempvoidexchangeso3、rt(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;}}voidbubblesort(vector&v){inttemp,n=v.size(),i,j;for(i=1;iv[j+1]){temp=v[j];v[j]=v[j+1];4、v[j+1]=temp;}}}templateintpivotIndex(vector&v,intfirst,intlast){intmid,scanUp,scanDown;Tpivot,temp;if(first==last)returnlast;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(scanUp5、own){while(scanUp<=scanDown&&v[scanUp]voidquicksort(vector&v,intfi6、rst,intlast){intpivotLoc;Ttemp;if(last-first<=1)return;elseif(last-first==2){if(v[last-1]voidmerge(vecto7、r&v,intfirst,intmid,intlast){vectortempVector;intindexA,indexB,indexV;indexA=first;indexB=mid;while(indexA
2、llIndex=j;if(smallIndex!=pass){temp=arr[pass];arr[pass]=arr[smallIndex];arr[smallIndex]=temp;}}}templatevoidinsertionSort(vector&v){inti,j,n=v.size();Ttemp;for(i=1;i0&&tempvoidexchangeso
3、rt(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;}}voidbubblesort(vector&v){inttemp,n=v.size(),i,j;for(i=1;iv[j+1]){temp=v[j];v[j]=v[j+1];
4、v[j+1]=temp;}}}templateintpivotIndex(vector&v,intfirst,intlast){intmid,scanUp,scanDown;Tpivot,temp;if(first==last)returnlast;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(scanUp5、own){while(scanUp<=scanDown&&v[scanUp]voidquicksort(vector&v,intfi6、rst,intlast){intpivotLoc;Ttemp;if(last-first<=1)return;elseif(last-first==2){if(v[last-1]voidmerge(vecto7、r&v,intfirst,intmid,intlast){vectortempVector;intindexA,indexB,indexV;indexA=first;indexB=mid;while(indexA
5、own){while(scanUp<=scanDown&&v[scanUp]voidquicksort(vector&v,intfi
6、rst,intlast){intpivotLoc;Ttemp;if(last-first<=1)return;elseif(last-first==2){if(v[last-1]voidmerge(vecto
7、r&v,intfirst,intmid,intlast){vectortempVector;intindexA,indexB,indexV;indexA=first;indexB=mid;while(indexA
此文档下载收益归作者所有