Cod sursa(job #959379)

Utilizator BlackLordFMI Alex Oprea BlackLord Data 8 iunie 2013 12:02:55
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int n, v[500010], i, m, x, aux, M, y;

void update(int x, int y){
	v[y]=x;
	m=y;
	M=m/2;
	while(v[m]<v[M] && m!=0)
	{
		aux=v[m];
		v[m]=v[M];
		v[M]=aux;
		m=M;
		M=m/2;
	}
}

void sterge(int &n){
	v[1]=v[n];
	n--;
	m=1;
	M=2;
	while(M <= n)
	{
		if (M + 1 <= n && v[M+1] < v[M])
			M++;
		
		if (v[m] > v[M]) {
			aux = v[m];
			v[m] = v[M];
			v[M] = aux;
		}
		
		m=M;
		M=m*2;
	}
}

int main(){
	f>>n;
	f>>x;
	v[1]=x;
	y=2;
	for(i=2; i<=n; i++)
	{
		f>>x;
		update(x, y);
		y++;
	}
	while(n>0)
	{
		g<<v[1]<<' ';
		sterge(n);
	}
	return 0;
}