Pagini recente » Cod sursa (job #188201) | Cod sursa (job #3165732) | Cod sursa (job #3287217) | Cod sursa (job #2505746) | Cod sursa (job #2483709)
#include <bits/stdc++.h>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
long long x,n,v[2*500000+10],l;
void up(int p)
{
if(p>1&&v[p]<v[p/2])
{
swap(v[p],v[p/2]);
up(p/2);
}
}
void down(int p)
{
if(p*2+1<=l)
{
if(v[p*2]<v[2*p+1]&&v[p*2]<v[p])
{
swap(v[2*p],v[p]);
down(2*p);
}
else if(v[2*p+1]<v[p*2]&&v[2*p+1]<v[p])
{
swap(v[2*p+1],v[p]);
down(2*p+1);
}
}
else if(v[2*p]<v[p]&&2*p<=l)
{
swap(v[2*p],v[p]);
down(2*p);
}
}
void deletee(int p)
{
swap(v[l],v[p]);
l--;
up(p);
down(p);
}
void add(int x)
{
v[++l]=x;
up(l);
}
int main()
{
f>>n;
for(int i=1;i<=n;i++)
{
f>>x;
add(x);
}
while(l)
{
g<<v[1]<<" ";
deletee(1);
}
return 0;
}