Cod sursa(job #825206)

Utilizator sleepaholicNeculaescu Theodor sleepaholic Data 27 noiembrie 2012 20:52:13
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#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(2*u<=j&&(heap[2*u]<heap[u]||((2*u+1<=j)&&heap[2*u+1]<heap[u]) ) )
{
if(2*u+1<=j&&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;
}