Pagini recente » Cod sursa (job #979690) | Cod sursa (job #2374828) | Cod sursa (job #206441) | Cod sursa (job #1888369) | Cod sursa (job #2805894)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int h[1000000], n;
int main()
{
fin >> n;
for(int i = 1; i <= n; i++)
fin >> h[i];
int dim_heap = 1;
for (int i = 2; i <= n; ++i)
{
dim_heap++;
int c = i, p = i/2;
while (p > 0 && h[c] > h[p])
{
swap(h[c], h[p]);
c = p;
p = c / 2;
}
}
while (dim_heap > 1)
{
swap(h[1], h[dim_heap]);
dim_heap--;
int p = 1, c = 2*p;
while (c <= dim_heap)
{
if (c + 1 <= dim_heap && h[c + 1] > h[c])
c++;
if (h[p] < h[c])
swap(h[p], h[c]);
else
break;
p = c;
c = 2 * p;
}
}
for(int i = 1; i <= n; i++)
fout << h[i] << " ";
return 0;
}