快排 [随机数]

#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<time.h>
using namespace std;
const int maxn = 100;
int inquicksort(int a[], int left, int right)
{
	int p = (int)(round(1.0 * rand() / RAND_MAX * (right - left) + left));
	swap(a[left], a[p]);
	int temp = a[left];
	while (left < right)
	{
		while (left<right&&a[right] > temp)
			right--;
		a[left] = a[right];
		while (left < right &&a[left] <=temp)
			left++;
		a[right] = a[left];
	}
	a[left] = temp;
	return left;
}
void quicksort(int a[], int left, int right)
{
	if (left < right)
	{
		int pos = inquicksort(a, left, right);
		quicksort(a, left, pos - 1);
		quicksort(a, pos + 1, right);
	}
}
int main()
{
	srand((unsigned)time(NULL));
	int a[maxn];
	int n; cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	quicksort(a, 0, n - 1);
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << " ";
	}
}
posted @ 2020-07-12 15:55  _Hsiung  阅读(84)  评论(0编辑  收藏  举报