Cod sursa(job #1314333)

Utilizator roberta9533Pavel Roberta roberta9533 Data 11 ianuarie 2015 19:32:08
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include<iostream>
#include<stdio.h>
#include<limits.h>
#include<fstream.h>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
long heap[500001];
int k,i,x,n;
void init()
{
    n=0;
    heap[0]=-INT_MAX;
}
void ins(int x)
{
    n++;
    heap[n]=x;
    int t=n;
    while(heap[t/2]>x)
    {
        heap[t]=heap[t/2];
        t/=2;
    }
    heap[t]=x;
}
int DeleteMin()
{
    int minim,last,c,t;
    minim=heap[1];
    last=heap[n--];
    for(t=1;t*2<=n;t=c)
    {
        c=t*2;
        if(c!=n&&heap[c+1]<heap[c]) c++;
        if(last>heap[c]) heap[t]=heap[c];
                else break;
    }
        heap[t]=last;
        return minim;
}
int main()
{
        f>>k;
        init();
        for(i=0;i<k;i++)
        {
            f>>x;
            ins(x);
        }
        for(i=0;i<k;i++)
            g<<DeleteMin()<<" ";
        return 0;
}