Pagini recente » Cod sursa (job #1691797) | Cod sursa (job #2563881) | Cod sursa (job #759129) | Cod sursa (job #3030988) | Cod sursa (job #2489443)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("algsort.in");
ofstream g ("algsort.out");
int heap[1000], n;
int getMin()
{
return heap[1];
}
int add(int x)
{
n++;
int poz=n;
heap[n]=x;
while(poz>1 && heap[poz/2]>heap[poz])
{
swap(heap[poz/2], heap[poz]);
poz/=2;
}
}
int popMin()
{
heap[1]=heap[n];
heap[n]=0;
int poz=1;
n--;
while(2*poz<=n)
{
poz*=2;
if(poz<n && heap[poz]>heap[poz+1]) poz++;
if(heap[poz] < heap[poz/2])
swap(heap[poz/2], heap[poz]);
else poz=n;
}
}
int m;
int main()
{
f>>m;
for(int i=1;i<=m;++i)
{
int y;
f>>y;
add(y);
}
for(int i=1;i<=m;++i)
{g<<getMin()<<" ";
popMin();
}
return 0;
}