Pagini recente » Istoria paginii runda/crrr3/clasament | Cod sursa (job #992147) | Istoria paginii runda/simulare-cartita-52b/clasament | Cod sursa (job #2094104) | Cod sursa (job #1684409)
#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 + rand() % (right - left);
swap(v[pivot], v[right - 1]);
int lindex = left, rindex = right - 2;
while (lindex <= rindex) {
if (v[lindex] <= v[pivot]) {
lindex++;
continue;
}
if (v[rindex] < v[lindex])
swap(v[rindex], v[lindex]);
rindex--;
}
swap(v[lindex], v[pivot]);
return pivot;
}
int quickFind(int left, int right, int val) {
if (left >= right - 1) return 0;
int p = partition(left, right);
quickFind(left, p, 0);
quickFind(p, right, 0);
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, 0);
for (int i = 0; i < N; i++)
printf("%d ", v[i]);
return 0;
}