Cod sursa(job #403725)

Utilizator ConsstantinTabacu Raul Consstantin Data 25 februarie 2010 11:02:01
Problema Sortare prin comparare Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include<stdio.h>

int h[ 600010 ],i,j,k,n;
void swap(int &a,int &b){
int aux;
aux = a;
a = b;
b = aux;
}

void del(){
int nod = 1,f1 = 2,f2 = 3;

while((h[nod] > h[f1] && f1<=k) ||  (h[nod] > h[f2] && f2 <= k))
	{if(f2 <= k)
	{if(h[f1] < h[f2])
		{swap(h[nod] , h[ f1 ]);
		nod = f1;
		f1 = 2*nod;
		f2 = 2*nod+1;
		}
	else
		{swap(h[nod],h[f2]);
		nod = f2;
		f1 = 2*nod;
		f2 = 2*nod+1;
		}
	}
	else
		{swap(h[nod] , h[ f1 ]);
		nod = f1;
		f1 = 2*nod;
		f2 = 2*nod+1;
		}
		
	}

}

void update(int nod){
while(h[nod] < h[nod>>1] && nod !=1)
	{swap(h[nod],h[nod>>1]);nod = nod>>1;
	}

}
int main(){
freopen("algsort.in","r",stdin);
freopen("algsort.out","w",stdout);

scanf("%d",&n);
for(i = 1 ; i <= n ; i++)
	{scanf("%d",&h[i]);
	update(i);
	}
k = n;
for(i = 1;i <= n ; i++)
	{printf("%d ",h[1]);
	h[1] = h[k--];
	del();}
return 0;}