Cod sursa(job #2287190)

Utilizator ValentinSavoiuFMI Savoiu Valentin-Marian ValentinSavoiu Data 21 noiembrie 2018 17:23:27
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <cstdio>
#include <fstream>
#define maxn 500010
using namespace std;
ifstream f ("algsort.in");
ofstream g ("algsort.out");
int N, L, NR;
int A[maxn];

void push(int x)
{
    while (x / 2 && A[x] > A[x / 2])
    {
        swap (A[x], A[x/2]);
        x /= 2;
    }
}

void pop(int x)
{
    int y = 0;
    while (x != y)
    {
        y = x;

        if (y * 2 <= L && A[x] < A[y * 2]) x = y  * 2;
        if (y * 2 + 1 <= L && A[x] < A[y * 2 + 1]) x = y * 2 + 1;

       swap(A[x], A[y]);
    }
}

int main()
{
    int i, x, cod;
    f >> N;
    for (i = 1; i <= N; i++)
        f >> A[i];
    for (i = 1; i <= N; i++)
        push(i);
    L = N;
    for(i = N; i >= 1; i--) {
        swap(A[1], A[i]);
        L--;
        pop(1);
    }
    for(i = 1; i <= N; i++)
        g << A[i] << ' ';
    return 0;
}