Mai intai trebuie sa te autentifici.
Cod sursa(job #820780)
Utilizator | Data | 21 noiembrie 2012 10:29:16 | |
---|---|---|---|
Problema | Sortare prin comparare | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.91 kb |
#include<fstream>
#include<vector>
using namespace std;
int n;
vector <int> h;
ifstream in("algsort.in");
ofstream out("algsort.out");
void upHeap(int k)
{
int aux;
if ((k>>1)==0)
return ;
if (h[k>>1]>h[k])
{
aux=h[k];
h[k]=h[k>>1];
h[k>>1]=aux;
upHeap(k>>1);
}
}
void solve()
{
int a;
h.push_back(0);
for (int i=1;i<=n;i++)
{
in>>a;
h.push_back(a);
h[0]++;
upHeap(h[0]);
}
}
void downHeap(int k)
{
int aux,fs,fd;
fs=k<<1;
fd=(k<<1)+1;
if (fd<=h[0])
{
if (h[fd]<h[fs])
{
aux=h[k];
h[k]=h[fd];
h[fd]=aux;
downHeap(fd);
return ;
}
}
if (fs<=h[0])
{
aux=h[k];
h[k]=h[fs];
h[fs]=aux;
downHeap(fs);
return ;
}
}
void print()
{
while (h[0])
{
out<<h[1]<<" ";
h[1]=h[h[0]];
h[0]--;
downHeap(1);
}
out<<"\n";
}
int main()
{
in>>n;
solve();
print();
return 0;
}