Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #1794976)
#include <stdio.h>
int h[5001], last=0;
inline void swap(int a, int b){
int aux;
aux=h[a];
h[a]=h[b];
h[b]=aux;
}
inline void add(int x){
h[++last]=x;
int poz=last;
while(poz>1 && h[poz]<h[poz/2])
swap(poz,poz/2),poz/=2;
}
inline void rm(int poz){
swap(poz,last--);
int ok=1, fs, fd, bun;
while(ok){
ok=0;
fs=2*poz;
fd=fs+1;
bun=poz;
if(fs<=last && h[fs]<h[bun])
bun=fs;
if(fd<=last && h[fd]<h[bun])
bun=fd;
if(bun!=poz){
ok=1;
swap(poz,bun);
poz=bun;
}
}
}
int main()
{
int i, n, x;
FILE *fi=fopen("algsort.in", "r"), *fo=fopen("algsort.out", "w");
fscanf(fi, "%d", &n);
for(i=1;i<=n;i++){
fscanf(fi, "%d", &x);
add(x);
}
for(i=1;i<=n;i++){
fprintf(fo, "%d ", h[1]);
rm(1);
}
fclose(fi);
fclose(fo);
return 0;
}