Pagini recente » Cod sursa (job #1495965) | Cod sursa (job #2212563) | Cod sursa (job #2076623) | Statistici UPB Mihalache Mistreanu Cosma (nudalapoli2015) | Cod sursa (job #2069073)
#include <fstream>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
long long n,i,j,v[500001],d,x;
void up(int p)
{
if(p>1&&v[p/2]>v[p])
{
swap(v[p],v[p/2]);
up(p/2);
}
}
void down(int p)
{
if(p*2+1<=d &&(v[p*2]<v[p]||v[p*2]+1<v[p]))
{if(v[p*2]<v[p*2+1])
{
swap(v[p],v[2*p]);
down(p*2);
}
else
{
swap(v[p],v[2*p+1]),down(2*p+1);
}}
else if(p*2<=d&&v[p*2]<v[p])
{
swap(v[p],v[2*p]);
}
}
void del(int p)
{
swap(v[p],v[d]);
d--;
up(p);
down(p);
}
int main()
{
f>>n;
d=0;
for(i=1; i<=n; i++)
{
f>>x;
v[++d]=x;
up(d);
}
while(d)
{
g<<v[1]<<" ";
del(1);
}
return 0;
}