Pagini recente » Cod sursa (job #3271261) | Cod sursa (job #2436220) | Cod sursa (job #10999) | Cod sursa (job #1476813) | Cod sursa (job #2068734)
#include <fstream>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int 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;
}