Cod sursa(job #1684440)

Utilizator mouse_wirelessMouse Wireless mouse_wireless Data 11 aprilie 2016 01:20:05
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;

#ifdef INFOARENA
#define ProblemName "algsort"
#endif

#define MCONCAT(A, B) A B
#ifdef ProblemName
#define InFile MCONCAT(ProblemName, ".in")
#define OuFile MCONCAT(ProblemName, ".out")
#else
#define InFile "fis.in"
#define OuFile "fis.out"
#endif

#define MAXN 500010
int v[MAXN];

int partition(int left, int right) {
	//int pivot = left + fastrand() % (right - left);
	int pivot = (left + right) >> 1;
	swap(v[pivot], v[right - 1]);
	pivot = v[right - 1];
	int lindex = left, rindex = right - 2;
	while (lindex <= rindex) {
		while (v[lindex] < pivot && lindex < right) ++lindex;
		while (v[rindex] > pivot && rindex >= left) --rindex;
		if (lindex <= rindex) {
			swap(v[rindex], v[lindex]);
			++lindex, --rindex;
		}
	}
	swap(v[lindex], v[right - 1]);
	return lindex;
}

//int K;
int quickFind(int left, int right) {
	if (left == right - 1) return 0;
	int p = partition(left, right);
	//if (p == K) return v[p];
	quickFind(left, p);
	quickFind(p, right);
	return 0;
}

int main() {
	assert(freopen(InFile, "r", stdin));
	assert(freopen(OuFile, "w", stdout));
	int N;
	scanf("%d", &N);
	for (int i = 0; i < N; i++)
		scanf("%d", &v[i]);
	quickFind(0, N);
	for (int i = 0; i < N; i++)
		printf("%d ", v[i]);
	return 0;
}