Mai intai trebuie sa te autentifici.

Cod sursa(job #1133185)

Utilizator DjokValeriu Motroi Djok Data 4 martie 2014 16:23:14
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include<fstream>
#include<algorithm>
using namespace std;
 int i,n,sf=1;
 long long heap[500001];
 
  void add1(int nod)
   {
       if (nod>1)
       if (heap[nod]>heap[nod/2]) {swap(heap[nod],heap[nod/2]); add1(nod/2); }
   }
   
  void cob(int nod)
  
  { int max1;
     max1=nod;
     if (2*nod<=sf && heap[nod]<heap[2*nod])  max1=2*nod;
     if (2*nod+1<=sf && heap[max1]<heap[2*nod+1]) max1=2*nod+1;
     if (nod!=max1) {swap(heap[nod],heap[max1]); cob(max1); }    
   }
   
 int main()
 {
   ifstream cin("algsort.in");
   ofstream cout("algsort.out");  
   cin>>n;
    for (i=1;i<=n;++i)
     cin>>heap[i];                     
                      
       
                     
    for (i=1;i<=n;++i)
    { add1(sf);
      sf++;
    }    
    --sf;
   
   for (i=1;i<=n;++i)
    {
     swap(heap[1],heap[sf]);
      sf--;
     cob(1);
    }
    
            for (i=1;i<=n;++i)
            cout<<heap[i]<<' ';
                    
                            
  
  return 0;              
 }
 //Totusi este trist in lume