Cod sursa(job #687952)

Utilizator predator5047Butiu Alexandru Octavian predator5047 Data 22 februarie 2012 21:29:41
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <iostream>
#include <fstream>
#define parent(nod) (nod)/2
#define left_child(nod) (nod)*2
#define right_child(nod) (nod)*2+1
using namespace std;

int h[500001],n;

void citire();

void shift(int k,int N)
{
    int son;
    do
    {
    	son = 0;
    	if(left_child(k) <= N)
    	{
            son = left_child(k);
            if(right_child(k) <= N && h[son] < h[right_child(k)])
                son = right_child(k);
            if(h[son] <= h[k])
                son = 0;
    	}
    	if(son)
        {
            swap(h[son],h[k]);
            k = son;
        }
    }while(son);
}

int main()
{
    citire();
    for(int i = n/2; i > 0; --i)
        shift(i,n);
    for(int i = n; i > 0; --i)
    {
        swap(h[1],h[i]);
        shift(1,i-1);
    }

    ofstream fout("algsort.out");

    for(int i = 1; i<=n; ++i)
        fout << h[i] <<' ';

    fout.close();
    return 0;
}

void citire()
{
    ifstream fin("algsort.in");
    fin >> n;
    for(int i = 1; i<=n; ++i)
        fin >> h[i];
    fin.close();
}