Pagini recente » Cod sursa (job #1179449) | Cod sursa (job #1811675) | Cod sursa (job #2051675) | Cod sursa (job #2335591) | Cod sursa (job #978874)
Cod sursa(job #978874)
#include <fstream>
#define Nmax 500001
#define In "algsort.in"
#define Out "algsort.out"
using namespace std;
int Heap[Nmax], n, N;
inline void CombHeap(const int Father)
{
int Son=2*Father;
if(Son>n)
return;
if(Son+1<=n && Heap[Son+1]>Heap[Son])
++Son;
if(Heap[Son]>Heap[Father])
{
swap(Heap[Son],Heap[Father]);
CombHeap(Son);
}
}
int main()
{
int i;
ifstream f(In);
f>>n;
N = n;
for(i = 1;i <= n; ++i)
f>>Heap[i];
f.close();
for(i = n/2; i; --i)
CombHeap(i);
ofstream g(Out);
for(; n; --n,CombHeap(1))
swap(Heap[1],Heap[n]);
for(i = 1;i <= N; ++i)
g<<Heap[i]<<" ";
g<<"\n";
g.close();
return 0;
}