Pagini recente » Cod sursa (job #1826057) | Cod sursa (job #1668994) | Cod sursa (job #25211) | Cod sursa (job #132120) | Cod sursa (job #2276886)
#include <bits/stdc++.h>
using namespace std;
ifstream in("algsort.in");
ofstream out("algsort.out");
int n, k, a[500100], aux[500100];
void dive(int st, int dr) {
if (st >= dr)
return;
int piv = a[(st + dr) >> 1];
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];
random_shuffle(a + 1, a + n + 1);
dive(1, n);
for (int i = 1; i <= n; i++)
out << a[i] << ' ';
return 0;
}