Pagini recente » Cod sursa (job #38926) | Cod sursa (job #2369823) | Cod sursa (job #725905) | Cod sursa (job #2699012) | Cod sursa (job #1047020)
#include <iostream>
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int heap[1000005],nr,k;
int i,x,tip;
void urca(int &n,int pozi)
{
while(pozi>1&& heap[pozi]<heap[pozi/2])
{
swap(heap[pozi],heap[pozi/2]);
pozi/=2;
}
}
void coboara(int &n,int pozi)
{
{
int poz1=0;
while(pozi!=poz1) {
poz1=pozi;
if(2*poz1<=n && heap[pozi]>heap[2*pozi])
pozi=2*poz1;
if(2*poz1+1<=n && heap[pozi]>heap[2*poz1+1])
pozi=2*poz1+1;
swap(heap[pozi],heap[poz1]);
}
}}
int main()
{int n=0;
f>>nr;
for(i=1;i<=nr;i++)
{
f>>x;
heap[++n]=x;
urca(n,n);
}
for(i=1;i<=nr;i++)
{
g<<heap[1]<<" ";
heap[1]=heap[n--];
coboara(n,1);
}
}