Cod sursa(job #1183868)

Utilizator ArchazeyBaltatu Andrei-Mircea Archazey Data 10 mai 2014 13:55:23
Problema Sortare prin comparare Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include<fstream>
using namespace std;

ifstream fin("algsort.in");
ofstream fout("algsort.out");

int n,a[500000],N;

inline void Insert(int x)
{
    int aux;
    a[++N]=x;
    aux=N;
    while ((aux>>1)>=1 && x<a[aux>>1])
        {
            swap(a[aux>>1],a[aux]);
            aux>>=1;
        }
}

inline void HeapSort(int &c)
{
    int son=1,test=0,father=1;
    a[1]=a[c];
    c--;
    while (!test)
        {
            son=father<<1;
            if (son<=n)
                {
                    if (son<n && a[son+1]<a[son]) son++;
                    if (a[son]<a[father])
                        {
                            swap(a[son],a[father]);
                            father=son;
                        }
                    else test=1;
                }
            else test=1;
        }
}

int main()
{
    int i,x;
    fin>>n;
    for (i=1;i<=n;i++)
        {
            fin>>x;
            Insert(x);
        }
    for (i=1;n;i++)
        {
            fout<<a[1]<<" ";
            HeapSort(n);
        }
    return 0;
}