Pagini recente » Cod sursa (job #2219717) | Cod sursa (job #2708744) | Cod sursa (job #490307) | Cod sursa (job #1054128) | Cod sursa (job #823656)
Cod sursa(job #823656)
#include <cstdio>
#include <algorithm>
const int MAXN = 500005;
using namespace std;
int L, H[MAXN], N;
void push(int x) {
while(x / 2 && H[x / 2] > H[x]) {
swap(H[x / 2], H[x]);
x /= 2;
}
}
void pop(int x) {
int y = 0;
while(x != y) {
y = x;
if(2 * y <= L && H[2 * y] < H[x]) x = 2 * y;
if(2 * y + 1 <= L && H[2 * y + 1] < H[x]) x = 2 * y + 1;
swap(H[y], H[x]);
}
}
int main() {
freopen("algsort.in", "r", stdin);
freopen("algsort.out", "w", stdout);
scanf("%d", &N);
int x;
for(int i = 1; i <= N; ++i) {
scanf("%d", &x);
H[i] = x;
push(i);
}
L = N;
for(int i = 1; i <= N; ++i) {
printf("%d ", H[1]);
swap(H[1], H[L--]);
pop(1);
}
return 0;
}