Pagini recente » Cod sursa (job #3183348) | Cod sursa (job #2344200) | Cod sursa (job #2874337) | Cod sursa (job #1206250) | Cod sursa (job #2287190)
#include <cstdio>
#include <fstream>
#define maxn 500010
using namespace std;
ifstream f ("algsort.in");
ofstream g ("algsort.out");
int N, L, NR;
int A[maxn];
void push(int x)
{
while (x / 2 && A[x] > A[x / 2])
{
swap (A[x], A[x/2]);
x /= 2;
}
}
void pop(int x)
{
int y = 0;
while (x != y)
{
y = x;
if (y * 2 <= L && A[x] < A[y * 2]) x = y * 2;
if (y * 2 + 1 <= L && A[x] < A[y * 2 + 1]) x = y * 2 + 1;
swap(A[x], A[y]);
}
}
int main()
{
int i, x, cod;
f >> N;
for (i = 1; i <= N; i++)
f >> A[i];
for (i = 1; i <= N; i++)
push(i);
L = N;
for(i = N; i >= 1; i--) {
swap(A[1], A[i]);
L--;
pop(1);
}
for(i = 1; i <= N; i++)
g << A[i] << ' ';
return 0;
}