Pagini recente » Cod sursa (job #586095) | Cod sursa (job #1581646) | Cod sursa (job #440167) | Cod sursa (job #281628) | Cod sursa (job #1228914)
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
#define N 500001
#define dim 8192
char ax[dim];
int pz;
inline void cit(int &x) {
x = 0;
while (ax[pz] < '0' || ax[pz] > '9')
if (++pz == dim)
fread(ax, 1, dim, stdin), pz = 0;
while (ax[pz] >= '0' && ax[pz] <= '9') {
x = x * 10 + ax[pz] - '0';
if (++pz == dim)
fread (ax, 1, dim, stdin), pz = 0;
}
}
int a[N], n;
int part(int a[], int l, int r) {
swap(a[r], a[ l + rand() % (r - l) + 1]);
int pivot = a[r], j = l - 1;
for (int i = l; i <= r; ++i)
if (a[i] <= pivot)
swap(a[i], a[++j]);
return j;
}
void quick(int a[], int l, int r) {
if (l >= r)
return;
int m = part(a, l, r);
quick(a, l, m - 1);
quick(a, m + 1, r);
}
int main() {
srand(time(0));
freopen ("algsort.in", "r", stdin);
freopen ("algsort.out", "w", stdout);
cit(n);
int i;
for (i = 1; i <= n; ++i) {
cit(a[i]);
}
quick(a, 1, n);
for (i = 1; i <= n;++i)
printf ("%d ", a[i]);
}