Pagini recente » Cod sursa (job #2681081) | Cod sursa (job #1048347) | Cod sursa (job #2606049) | Cod sursa (job #2744501) | Cod sursa (job #846508)
Cod sursa(job #846508)
#include<fstream>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int n,x[500001];
void quicksort(int a[500001], int l, int r)
{ int i = l-1, j = r, p = l-1, q = r ,k; int v = a[r];
if (r <= l) return;
for (;;)
{
while (a[++i] < v) ;
while (v < a[--j]) if (j == l) break;
if (i >= j) break;
swap(a[i], a[j]);
if (a[i] == v) { p++; swap(a[p], a[i]); }
if (v == a[j]) { q--; swap(a[j], a[q]); }
}
swap(a[i], a[r]); j = i-1; i = i+1;
for (k = l; k < p; k++, j--) swap(a[k], a[j]);
for (k = r-1; k > q; k--, i++) swap(a[i], a[k]);
quicksort(a, l, j);
quicksort(a, i, r);
}
int main()
{int i;
f>>n;
for(i=1;i<=n;i++) f>>x[i];
quicksort(x,1,n);
for(i=1;i<=n;i++) g<<x[i]<<" ";
return 0;
}