Pagini recente » Cod sursa (job #58419) | Cod sursa (job #1083396) | Cod sursa (job #1391230) | Cod sursa (job #1101583) | Cod sursa (job #527349)
Cod sursa(job #527349)
#include<fstream>
#include<algorithm>
#define NMAX 500005
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int heap[NMAX], n, i, x;
void insert(int f)
{
int t=f/2;
while (heap[t]>heap[f])
{
swap(heap[t], heap[f]);
f=t; t=f/2;
}
}
void coboara(int f)
{
int t=1, imn;
while ( (t*2<=n && heap[t*2]<heap[t]) || (t*2+1<=n && heap[t*2+1]<=heap[t]) )
{
f=t*2; imn=f;
if (f+1<=n && heap[f]>heap[f+1]) imn=f+1;
swap(heap[imn], heap[t]);
t=imn;
}
}
int main()
{
f>>n>>heap[1];
for (i=2; i<=n; ++i)
{
f>>heap[i];
insert(i);
}
while(n)
{
g<<heap[1]<<" ";
heap[1]=heap[n];
--n; coboara(1);
}
f.close();
g.close();
return 0;
}