Pagini recente » Cod sursa (job #2503548) | Cod sursa (job #1704721) | Cod sursa (job #2086324) | Cod sursa (job #138284) | Cod sursa (job #1314333)
#include<iostream>
#include<stdio.h>
#include<limits.h>
#include<fstream.h>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
long heap[500001];
int k,i,x,n;
void init()
{
n=0;
heap[0]=-INT_MAX;
}
void ins(int x)
{
n++;
heap[n]=x;
int t=n;
while(heap[t/2]>x)
{
heap[t]=heap[t/2];
t/=2;
}
heap[t]=x;
}
int DeleteMin()
{
int minim,last,c,t;
minim=heap[1];
last=heap[n--];
for(t=1;t*2<=n;t=c)
{
c=t*2;
if(c!=n&&heap[c+1]<heap[c]) c++;
if(last>heap[c]) heap[t]=heap[c];
else break;
}
heap[t]=last;
return minim;
}
int main()
{
f>>k;
init();
for(i=0;i<k;i++)
{
f>>x;
ins(x);
}
for(i=0;i<k;i++)
g<<DeleteMin()<<" ";
return 0;
}