Cod sursa(job #1047020)

Utilizator alexsuciuAlex Suciu alexsuciu Data 3 decembrie 2013 20:21:30
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include<fstream>
#include<algorithm>

using namespace std;

ifstream f("algsort.in");
ofstream g("algsort.out");
int heap[1000005],nr,k;
int i,x,tip;

void urca(int &n,int pozi)
{
    while(pozi>1&& heap[pozi]<heap[pozi/2])
    {
        swap(heap[pozi],heap[pozi/2]);
        pozi/=2;

    }
}

void coboara(int &n,int pozi)
{
 {
    int poz1=0;

    while(pozi!=poz1) {
        poz1=pozi;

        if(2*poz1<=n && heap[pozi]>heap[2*pozi])
            pozi=2*poz1;
        if(2*poz1+1<=n && heap[pozi]>heap[2*poz1+1])
            pozi=2*poz1+1;

        swap(heap[pozi],heap[poz1]);
        }
}}

int main()
{int n=0;
    f>>nr;
    for(i=1;i<=nr;i++)
    {
        f>>x;
        heap[++n]=x;
        urca(n,n);
    }
    for(i=1;i<=nr;i++)
    {
        g<<heap[1]<<" ";
        heap[1]=heap[n--];
        coboara(n,1);
    }

}