Cod sursa(job #1262333)

Utilizator CostanMiriamCostan Miriam CostanMiriam Data 13 noiembrie 2014 08:21:14
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>

using namespace std;

ifstream fin ("algsort.in");
ofstream fout ("algsort.out");

int h[500010],n,i,x,p,c,aux,k;

void insert (int x) {
    h[++k]=x;
    c=k;
    p=k/2;
    while (p>0) {
        if (h[p]>h[c]) {
            aux= h[c];
            h[c]=h[p];
            h[p]=aux;
            c=p;
            p/=2;
        }else
            break;
    }
}

void delete_root() {
    h[1]=h[k];
    k--;
    p=1;
    c=2;
    while (c <= k) {
        if (c+1<=k && h[c+1]<h[c])
            c++;
        if (h[p]>=h[c]){
            aux=h[c];
            h[c]=h[p];
            h[p]=aux;
            p=c;
            c*=2;
        }else
            break;
    }
}

int main () {

    fin>>n;

    for (i=1;i<=n;i++){
        fin>>x;
        insert (x);
    }
    for (i=1;i<=n;i++) {
        fout<<h[1]<<" ";
        delete_root();
    }

    return 0;
}