#include<iostream>#include<ctime>#include<cstdlib>using namespace std; void Merge(int Array[],int TempArray[],int low,int high,int mid); void MergeSort(int Array[],int TempArray[],int low,int high) { int mid; if(low<high) { mid=(low+high)/2; MergeSort(Array,TempArray,low,mid); MergeSort(Array,TempArray,mid+1,high); Merge(Array,TempArray,low,high,mid); } } void Merge(int Array[],int TempArray[],int low,int high,int mid) { int i,j,index1,index2; for(j=low;j<high;j++) TempArray[j]=Array[j]; index1=low; index2=mid+1; i=low; while(index1<=mid&&index2<=high) { if(TempArray[index1]<=TempArray[index2]) Array[i++]=TempArray[index2++]; else Array[i++]=TempArray[index2++]; } while(index1<=mid) Array[i++]=TempArray[index1++]; while(index2<=high) Array[i++]=TempArray[index2++]; } void QuickSort(int Array[], int left, int right) { if(left < right) { int key = Array[left]; int low = left; int high = right; while(low < high) { while(low < high && Array[high] >= key) high--; Array[low] = Array[high]; while(low < high && Array[low] <= key) low++; Array[high] = Array[low]; } Array[low] = key; QuickSort(Array,left,low-1); QuickSort(Array,low+1,right); }}void SelectSort(int Array[],int low,int high){ int i,j; for(i=low;i<high;i++) { int pos=i; for(j=i;j<high;j++) if(Array[pos]>Array[j]) pos=j; int temp; temp=Array[pos]; Array[pos]=Array[i]; Array[i]=temp; }}void main(){ const int MAX=100000;//随机数取值范围0到100000 const int SIZE=100000;//最多产生20*10000个数据 cout<<"sort "<<SIZE<<" numble..."<<endl; int Array[SIZE]; int TempArray[SIZE]; srand(time(NULL));//srand()产生一个以当前时间的随机种子 cout<<"the num of testing:"; for(int i=0;i<SIZE;i++) { Array[i]=rand()%MAX;//数值取0到100000 //cout<<Array[i]<<" "; } cout<<endl; time_t start,end;//用以对算法时间的计时 start=clock(); QuickSort(Array,0,SIZE-1); end=clock(); cout<<"QuickSort cost(/ms):"<<difftime(end,start)<<endl; start=clock(); MergeSort(Array,TempArray,0,SIZE-1); end=clock(); cout<<"MergeSort cost(/ms):"<<difftime(end,start)<<endl; SelectSort(Array,0,SIZE-1); end=clock(); cout<<"SelectSort cost(/ms):"<<difftime(end,start)<<endl;}