Pagini recente » Cod sursa (job #675906) | Cod sursa (job #2953069) | Cod sursa (job #138355) | Cod sursa (job #1573619) | Cod sursa (job #2071181)
#include <fstream>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int n,l,h[500001];
void up (int p)
{
if(p>1&&h[p/2]>h[p])
{
swap(h[p],h[p/2]);
up(p/2);
}
}
void down (int p)
{
if(2*p+1<=l&&(h[p*2]<h[p]||h[2*p+1]<h[p]))
{
if(h[2*p]<h[2*p+1])
{
swap(h[p],h[p*2]);
down(p*2);
}
else
{
swap(h[2*p+1],h[p]);
down(p*2+1);
}
}
else if(2*p<=l&&h[p*2]<h[p])swap(h[p*2],h[p]);
}
void adaug(int x)
{
h[++l]=x;
up(l);
}
void del (int p)
{
swap(h[p],h[l]);
l--;
up(p);
down(p);
}
int i,x;
int main()
{
f>>n;
for(i=1;i<=n;i++)
{
f>>x;
adaug(x);
}
while(l)
{
g<<h[1]<<" ";
del(1);
}
return 0;
}