Pagini recente » Cod sursa (job #870366) | Cod sursa (job #899690) | Cod sursa (job #674864) | Cod sursa (job #2159475) | Cod sursa (job #880173)
Cod sursa(job #880173)
// Include
#include <fstream>
#include <algorithm>
using namespace std;
// Definitii
#define father node/2
#define leftSon node*2
#define rightSon node*2 + 1
// Constante
const int sz = (int)5e5+1;
//const int sz = 101;
// Functii
void checkUp(int node);
// Variabile
ifstream in("algsort.in");
ofstream out("algsort.out");
int heapSize, num;
int heap[sz];
// Main
int main()
{
in >> num;
heapSize = num;
for(int i=1 ; i<=num ; ++i)
{
in >> heap[i];
checkUp(i);
}
while(heapSize != 1)
{
swap(heap[1], heap[heapSize--]);
for(int i=1 ; i<=heapSize ; ++i)
checkUp(i);
}
for(int i=1 ; i<=num ; ++i)
out << heap[i] << ' ';
in.close();
out.close();
return 0;
}
void checkUp(int node)
{
if(node==1 || node > heapSize)
return;
if(heap[father] < heap[node])
{
swap(heap[father], heap[node]);
checkUp(father);
}
}