Cod sursa(job #978874)

Utilizator narcis_vsGemene Narcis - Gabriel narcis_vs Data 29 iulie 2013 22:32:54
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <fstream>

#define Nmax 500001
#define In "algsort.in"
#define Out "algsort.out"

using namespace std;

int Heap[Nmax], n, N;

inline void CombHeap(const int Father)
{
    int Son=2*Father;
    if(Son>n)
        return;
    if(Son+1<=n && Heap[Son+1]>Heap[Son])
        ++Son;
    if(Heap[Son]>Heap[Father])
    {
        swap(Heap[Son],Heap[Father]);
        CombHeap(Son);
    }
}
int main()
{
    int i;
    ifstream f(In);
    f>>n;
    N = n;
    for(i = 1;i <= n; ++i)
        f>>Heap[i];
    f.close();
    for(i = n/2; i; --i)
        CombHeap(i);
    ofstream g(Out);
    for(; n; --n,CombHeap(1))
        swap(Heap[1],Heap[n]);
    for(i = 1;i <= N; ++i)
        g<<Heap[i]<<" ";
    g<<"\n";
    g.close();
    return 0;
}