Pagini recente » Cod sursa (job #3162465) | Cod sursa (job #803519)
Cod sursa(job #803519)
#include <fstream>
#define maxN 500005
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int n, a[maxN];
void read()
{
f >> n;
for (int i = 1; i <= n; i++)
f >> a[i];
}
void quicksort(int st, int dr)
{
int i = st, j = dr, pivot;
pivot = a[(i + j) / 2];
while (i <= j) {
while (a[i] < pivot)
i++;
while (a[j] > pivot)
j--;
if (i <= j) {
int aux = a[i];
a[i] = a[j];
a[j] = aux;
i++;
j--;
}
}
if (i < dr)
quicksort(i, dr);
if (st < j)
quicksort(st, j);
}
int main()
{
read();
quicksort(1, n);
for (int i = 1; i <= n; i++)
g << a[i] << " ";
f.close();
g.close();
return 0;
}