Pagini recente » Cod sursa (job #2401733) | Cod sursa (job #1671344) | Cod sursa (job #2910144) | Cod sursa (job #3191768) | Cod sursa (job #403719)
Cod sursa(job #403719)
#include<stdio.h>
int h[ 500010 ],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])
{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;}