Cod sursa(job #823656)

Utilizator mathboyDragos-Alin Rotaru mathboy Data 25 noiembrie 2012 14:45:28
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#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;
}