Pagini recente » Cod sursa (job #1997200) | Cod sursa (job #1711987) | Cod sursa (job #222558) | Arhiva de probleme | Cod sursa (job #1815972)
#include <iostream>
#include <fstream>
#include <stdlib.h>
#define NMAX 500001
#define BUFF_SIZE 100001
using namespace std;
ifstream in("algsort.in");
ofstream out("algsort.out");
int A[NMAX], n, pos = 0;
char buff[BUFF_SIZE];
void Read(int &a)
{
while (!isdigit(buff[pos]))
if (++pos == BUFF_SIZE)
in.read(buff, BUFF_SIZE), pos = 0;
a = 0;
while (isdigit(buff[pos]))
{
a = a * 10 + (buff[pos] - '0');
if (++pos == BUFF_SIZE)
in.read(buff, BUFF_SIZE), pos = 0;
}
}
void quicksort(int x, int y)
{
if (x < y)
{
int i = x, j = y;
int Loc = x;
while (i < j)
{
while (A[Loc] <= A[j] && j != Loc)
j--;
if (A[Loc] > A[j])
{
swap(A[Loc], A[j]);
Loc = j;
}
while (A[i] <= A[Loc] && i != Loc)
i++;
if (A[i] > A[Loc])
{
swap(A[Loc], A[i]);
Loc = i;
}
}
int m = i;
quicksort(x, m - 1);
quicksort(m + 1, y);
}
}
int main()
{
Read(n);
for (int i = 1; i <= n; i++)
Read(A[i]);
in.close();
quicksort(1, n);
for (int i = 1; i <= n; i++)
out << A[i] << " ";
out.close();
return 0;
}