Pagini recente » Cod sursa (job #1918255) | Cod sursa (job #697175) | Cod sursa (job #2886247) | Cod sursa (job #1776395) | Cod sursa (job #687952)
Cod sursa(job #687952)
#include <iostream>
#include <fstream>
#define parent(nod) (nod)/2
#define left_child(nod) (nod)*2
#define right_child(nod) (nod)*2+1
using namespace std;
int h[500001],n;
void citire();
void shift(int k,int N)
{
int son;
do
{
son = 0;
if(left_child(k) <= N)
{
son = left_child(k);
if(right_child(k) <= N && h[son] < h[right_child(k)])
son = right_child(k);
if(h[son] <= h[k])
son = 0;
}
if(son)
{
swap(h[son],h[k]);
k = son;
}
}while(son);
}
int main()
{
citire();
for(int i = n/2; i > 0; --i)
shift(i,n);
for(int i = n; i > 0; --i)
{
swap(h[1],h[i]);
shift(1,i-1);
}
ofstream fout("algsort.out");
for(int i = 1; i<=n; ++i)
fout << h[i] <<' ';
fout.close();
return 0;
}
void citire()
{
ifstream fin("algsort.in");
fin >> n;
for(int i = 1; i<=n; ++i)
fin >> h[i];
fin.close();
}