Cod sursa(job #2489443)

Utilizator RadianElevenAdrian Ariotn RadianEleven Data 8 noiembrie 2019 19:47:22
Problema Sortare prin comparare Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("algsort.in");
ofstream g ("algsort.out");
int heap[1000], n;
int getMin()
{
    return heap[1];
}
int add(int x)
{

    n++;
    int poz=n;
    heap[n]=x;
    while(poz>1 && heap[poz/2]>heap[poz])
    {
        swap(heap[poz/2], heap[poz]);
        poz/=2;
    }
}
int popMin()
{

    heap[1]=heap[n];
    heap[n]=0;
    int poz=1;
    n--;
    while(2*poz<=n)
    {
        poz*=2;
        if(poz<n && heap[poz]>heap[poz+1]) poz++;
        if(heap[poz] < heap[poz/2])
            swap(heap[poz/2], heap[poz]);
        else poz=n;
    }
}
int m;

int main()
{
    f>>m;
    for(int i=1;i<=m;++i)
    {
        int y;
        f>>y;

        add(y);
    }
    for(int i=1;i<=m;++i)
    {g<<getMin()<<" ";
        popMin();

    }
    return 0;
}