Cod sursa(job #2068714)

Utilizator cicero23catalin viorel cicero23 Data 18 noiembrie 2017 10:38:33
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int h[500001];
int n;
void up(int p)
{
    if(p>1&&h[p/2]<h[p])
    {
        swap(h[p],h[p/2]);
        up[p/2];
    }
}
void add(int x)
{
    h[++n]=x;
    up(n);
}
void down(int p)
{
    if(p*2+1<=n&&(h[2*p]>h[p]||h[2*p+1]>h[p]))
    {
        if(h[2*p]>h[2*p+1]){swap(h[p],h[2*p]);down(2*p);}
        else {swap(h[p],h[2*p+1]);down(2*p+1);}
    }
    else
        if(p*2<=n&&h[2*p]>h[p])
        {
            swap(h[p],h[2*p]);
        }
}
void dell(int p)
{
    swap(h[p],h[n]);
    n--;
    up[p];
    down(p);
}
int main()
{
    int i,x,t;
    f>>t;
    for(i=1;i<=t;i++)
        {
            f>>x;
            add(x);
        }

    while(n)
    {
        g<<h[1]<<" ";
        dell(1);
    }
    return 0;
}