Cod sursa(job #2030993)

Utilizator circeanubogdanCirceanu Bogdan circeanubogdan Data 2 octombrie 2017 16:36:51
Problema Sortare prin comparare Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
#define DIM 500002

using namespace std;

ifstream f("algsort.in");
ofstream g("algsort.out");

int n, h[DIM], x, nn;

void percolate(int val)
{
    h[++nn] = val;
    int c = nn;
    int p = nn / 2;
    while(p >= 1 && h[c] > h[p])
    {
        swap(h[c], h[p]);
        c = p;
        p = c / 2;
    }
}

void sift(int nod)
{
    swap(h[nn], h[nod]);
    -- nn;
    int p = nod;
    int c = 2 * p;
    if(h[c + 1] > h[c]) ++ c;
    while(h[c] > h[p] && c <= nn)
    {
        swap(h[c], h[p]);
        p = c;
        c = 2 * p;
        if(h[c + 1] > h[c]) ++ c;
    }
}

int main()
{
    f>>n;
    for(int i = 1; i <= n; ++ i)
    {
        f>>x;
        percolate(x);
    }

    for(int i = 1; i <= n; ++ i)
        sift(1);

    for(int i = 1; i <= n; ++ i)
        g<<h[i]<<" ";

    return 0;
}