Cod sursa(job #2845400)

Utilizator Theo14Ancuta Theodor Theo14 Data 7 februarie 2022 19:33:30
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include<bits/stdc++.h>
using namespace std;

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

int heap[500002],n,x,n_heap;

void add(int value)
{
    n_heap++;
    heap[n_heap]=value;
    int current=n_heap;
    while(current>1 && heap[current]<heap[current/2])
    {
        swap(heap[current],heap[current/2]);
        current/=2;
    }
}

void removee()
{
    heap[1]=heap[n_heap];
    heap[n_heap]=0;
    n_heap--;
    int current=2,p=1;
    while(current<=n_heap)
    {
        if(current+1<=n_heap && heap[current+1]<heap[current])
            current++;
        if(heap[current]<heap[p])
            swap(heap[current],heap[p]);
        p=current;
        current=2*p;
    }
}

int main()
{
    int i;
    f>>n;
    for(i=1;i<=n;i++)
    {
        f>>x;
        add(x);
    }
    for(i=1;i<=n;i++)
    {
        g<<heap[1]<<" ";
        removee();
    }
    return 0;
}