Cod sursa(job #1484674)

Utilizator iulianrotaruRotaru Gheorghe-Iulian iulianrotaru Data 11 septembrie 2015 17:14:25
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int n,v,i,heap[500012];
void push(int x)
{
    heap[++heap[0]] = x;
    int now = heap[0], next = heap[0] >> 1;
    while (next >= 1)
    {
        if (heap[now] < heap[next])
        {
            swap(heap[now], heap[next]);
            now >>= 1, next >>= 1;
        }
        else break;
    }
}
void pop()
{
    heap[1] = heap[heap[0]--];
    int now = 1, next = 2;
    while (next <= heap[0])
    {
        if (next < heap[0] && heap[next] > heap[next + 1])
            ++next;
        if (heap[now] > heap[next])
        {
            swap(heap[now], heap[next]);
            now = next, next <<= 1;
        }
        else break;
    }
}
int main()
{
    f>>n;
    for(i=1;i<=n;++i) f>>v,push(v);
    while(n--) g<<heap[1]<<" ",pop();
    return 0;
}