Pagini recente » Rating Popescu Ion (2H2O) | Cod sursa (job #668593) | Cod sursa (job #1955851) | Cod sursa (job #1873587) | Cod sursa (job #824761)
Cod sursa(job #824761)
#include<fstream>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int heap[600000];
int j=0,aux;
void add()
{
int x;
fin>>x;
heap[++j]=x;
int j2;
j2=j;
while(j>1&&heap[j/2]>heap[j])
{
aux=heap[j/2];
heap[j/2]=heap[j];
heap[j]=aux;
j=j/2;
}
j=j2;
}
void remove()
{ int u,p;
fout<<heap[1]<<" ";
heap[1]=heap[j];
heap[j]=0;
j--;
u=1;
while(heap[2*u]&&(heap[2*u]<heap[u]||(heap[2*u+1]&&heap[2*u+1]<heap[u])))
{
if(heap[2*u+1]&&heap[2*u+1]<heap[2*u])p=2*u+1;
else p=2*u;
aux=heap[p];
heap[p]=heap[u];
heap[u]=aux;
u=p;
}
}
int main()
{
int n,i;
fin>>n;
for(i=1;i<=n;i++)
add();
for(i=1;i<=n;i++)
remove();
return 0;
}