(算法设计技巧与分析)QuickSort

来源:互联网 发布:证券投资分析软件 编辑:程序博客网 时间:2024/06/13 21:44

#include<iostream>#include<time.h>#include<Windows.h>using namespace std;int Split(int a[],int low,int high);void QuickSort(int a[],int low,int high);int main(){int a[1100]={4,6,3,1,8,7,2,5};double start,end;srand((unsigned int(time(NULL))));//导入时间种子加每次运行结样试下伪随机数种子for (int i=0;i<1100;i++)  a[i]=rand();start=GetTickCount();  QuickSort(a,0,1099);end=GetTickCount();  for(int i=0;i<1100;i++)cout<<a[i]<<" ";cout<<endl;cout<<end-start<<endl;return 0;}int Split(int a[],int low,int high){//选第一个数为参照数,排序结果a[low]左侧都比a[low]少,a[high]右侧都比a[low]大int i=low,temp;for(int j=low+1;j<=high;j++)if(a[j]<a[low]){i++;if(i!=j){temp=a[i];a[i]=a[j];a[j]=temp;}}temp=a[i];a[i]=a[low];a[low]=temp;return i;}void QuickSort(int a[],int low,int high){if(low<high){int w=Split(a,low,high);QuickSort(a,low,w-1);QuickSort(a,w+1,high);}}


0 0
原创粉丝点击