Pagini recente » Cod sursa (job #2887715) | Cod sursa (job #3249909) | Cod sursa (job #2600471) | Cod sursa (job #2970017) | Cod sursa (job #1973738)
#include <cstdio>
const int MAX_N = 500000;
int v[ MAX_N ];
void qsort(int begin, int end) {
int b, e, pivot, aux;
b = begin;
e = end;
pivot = v[(b + e) / 2];
while(b <= e) {
while(v[b] < pivot) b++;
while(v[e] > pivot) e--;
if(b<=e) {
aux = v[b];
v[b] = v[e];
v[e] = aux;
b++;
e--;
}
}
if(begin < e)
qsort(begin, e);
if(b < end)
qsort(b, end);
}
int main() {
int n, i;
FILE *fin = fopen("algsort.in", "r");
fscanf(fin, "%d", &n);
for(i = 0; i < n; i++)
fscanf(fin, "%d", &v[i]);
fclose(fin);
qsort(0, n - 1);
FILE *fout = fopen("algsort.out", "w");
for(i = 0; i < n; i++)
fprintf(fout, "%d ", v[i]);
fclose(fout);
return 0;
}