Pagini recente » Cod sursa (job #501423) | Cod sursa (job #864328) | Cod sursa (job #524213) | Cod sursa (job #1643310) | Cod sursa (job #2943023)
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int n, x;
vector<int> heap;
const int inf = 0x3F3F3F3F;
void heapify(int root){
int l = 2*root+1;
int r = 2*root+2;
int minim = root;
if(l<heap.size() && heap[l] < heap[minim]) minim = l;
if(r<heap.size() && heap[r] < heap[minim]) minim = r;
if(minim != root){
swap(heap[root], heap[minim]);
heapify(minim);
}
}
int main(){
fin>>n;
for(int i=1; i<=n; i++){
fin>>x;
heap.push_back(x);
}
for(int i = (heap.size()-1)/2; i>=0; i--) heapify(i);
while(heap.size()){
fout<<heap[0]<<" ";
swap(heap[0], heap[heap.size()-1]);
heap.pop_back();
heapify(0);
}
}