Pagini recente » Cod sursa (job #941192) | Cod sursa (job #1120781) | Cod sursa (job #2890718) | Cod sursa (job #1257477) | Cod sursa (job #752145)
Cod sursa(job #752145)
#include <cstdio>
#include <algorithm>
using namespace std;
int n,h[500005],k;
void push(int x){
h[++k] = x;
for(int t=k; t/2>0 && h[t/2] > h[t] ;t/=2) swap(h[t/2],h[t]);
}
void pop(){
int f;
h[1] = h[k--];
if(h[2] < h[3]) f = 2; else f = 3;
for(int t=1; f <= k && h[t] > h[f] ; t=f, h[2*t] > h[2*t+1] ? f = 2*t : f = 2*t+1) swap(h[t],h[f]);
}
int main(){
int x;
freopen("algsort.in","r",stdin);
freopen("algsort.out","w",stdout);
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&x);
push(x);
}
for(int i=0;i<n;i++)
{
printf("%d ",h[1]);
pop();
}
return 0;
}