Pagini recente » Cod sursa (job #69672) | Cod sursa (job #1678672) | Cod sursa (job #659512) | Cod sursa (job #464178) | Cod sursa (job #1484674)
#include <fstream>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int n,v,i,heap[500012];
void push(int x)
{
heap[++heap[0]] = x;
int now = heap[0], next = heap[0] >> 1;
while (next >= 1)
{
if (heap[now] < heap[next])
{
swap(heap[now], heap[next]);
now >>= 1, next >>= 1;
}
else break;
}
}
void pop()
{
heap[1] = heap[heap[0]--];
int now = 1, next = 2;
while (next <= heap[0])
{
if (next < heap[0] && heap[next] > heap[next + 1])
++next;
if (heap[now] > heap[next])
{
swap(heap[now], heap[next]);
now = next, next <<= 1;
}
else break;
}
}
int main()
{
f>>n;
for(i=1;i<=n;++i) f>>v,push(v);
while(n--) g<<heap[1]<<" ",pop();
return 0;
}