Pagini recente » Cod sursa (job #1372132) | Monitorul de evaluare | Cod sursa (job #2417517) | Cod sursa (job #1311894) | Cod sursa (job #2276831)
#include <bits/stdc++.h>
using namespace std;
ifstream in("sdo.in");
ofstream out("sdo.out");
int n, k, a[500100], aux[500100];
void dive(int st, int dr) {
// cout << st << ' ' << dr << '\n';
// int w;
// cin >> w;
if (st >= dr)
return;
int piv = rand() % (dr - st + 1) + st;
piv = a[piv];
int l = st, r = dr;
for (int i = st; i <= dr; i++)
if (a[i] < piv)
aux[l++] = a[i];
else if (a[i] > piv)
aux[r--] = a[i];
for (int i = st; i <= dr; i++)
if (a[i] == piv)
aux[l++] = a[i];
for (int i = 1; i <= n; i++)
a[i] = aux[i];
r++;
l--;
dive(l + 1, dr);
dive(st, l - 1);
}
int main() {
ios_base::sync_with_stdio(0);
in.tie(NULL);
srand(time(NULL));
in >> n;
for (int i = 1; i <= n; i++)
in >> a[i];
dive(1, n);
for (int i = 1; i <= n; i++)
out << a[i] << ' ';
return 0;
}