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

#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);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值