Cod sursa(job #778952)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 16 august 2012 12:36:35
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>
#include <cmath>
#include <algorithm>
using namespace std;
int h[2000],i,n,k;
void heapup( int i )
{
    int aux;
    if (i>1)
        if (h[i]>h[i/2])
        {
            aux=h[i];
            h[i]=h[i/2] ;
            h[i/2]=aux;
            heapup(i/2);
        }
}
void heapdown ( int i, int n )
{
    int st,dr,p;
    if ( i*2<=n )
   {
       st=h[2*i];
       if (i*2+1<=n) dr=h[2*i+1];
          else dr=st-1;
       if (st>dr) p=2*i; else p=2*i+1;
       swap(h[p],h[i]);
       heapdown(p,n);
   }
}
int main()
{
    ifstream f("algsort.in");
    ofstream g("algsort.out");
    f>>n;
    for (i=1; i<=n; i++)
    {
        f>>h[i];
        heapup(i);
      } int x=n;
    for (i=1;i<x;i++)
    {
        swap(h[1],h[n]);
        n--;
        heapdown(1,n);}
    for (i=1;i<=x;i++) g<<h[i]<<" ";
    f.close();
    g.close();
    return 0;
}