#P1310. 快速排序

快速排序

Description

用快速排序方法对N<200000个整数从小到大排序。

#include <iostream>  
#define MAXN 200000
using namespace std;
int data[MAXN];
int n;

void sort(int l, int r) {
	int x = data[(l + r) / 2], i = l, j = r, temp;
	while (i <= j) {
		while (data[i] < x) i++;
		while (data[j] > x) j--;

		if (_____(1)_____) {
			temp = data[i];
			data[i] = data[j];
			data[j] = temp;
			i++;
			j--;
		}
	}
	if (i < r) sort(i, r);
	if (l < j) sort(___(2)____, _____(3)_____);
}

int main() {
	cin >> n;
	int i;
	for (i = 1; i <= n; i++) {
		scanf("%d",&data[i]);
	}
	sort(___(4)___,____(5)_____);
	for (i = 1; i <= n; i++)
		printf("%d\n",data[i]);
	return 0;
}

Input Format

10
4 85  3 234 45 345 345 122 30 12

Output Format

3
4
12
30
45
85
122
234
345
345

Source

排序