您的位置:首页 > 产品设计 > UI/UE

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

2015-03-10 22:16 267 查看


#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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: