Pagini recente » Statistici Szabo Maria (szabomaria) | Cod sursa (job #2939033) | Cod sursa (job #1265498) | Cod sursa (job #2964569) | Cod sursa (job #1850537)
#include <iostream>
#include <fstream>
#include <algorithm>
#define MAX_HEAP_SIZE 500001
using namespace std;
typedef int Heap[MAX_HEAP_SIZE];
int ordine[MAX_HEAP_SIZE], N,p;
Heap H;
inline int father(int nod){
return nod >> 1;
}
inline int left_son(int nod){
return nod << 1;
}
inline int right_son(int nod){
return (nod << 1) + 1;
}
//Coboram nodul K in heap pana ajunge pe o pozitie potrivita
void sift(int K) {
int son;
do {
son = 0;
if (left_son(K) <= N) {
son = left_son(K);
if (right_son(K) <= N && H[right_son(K)] < H[left_son(K)]) {
son = right_son(K);
}
if (H[son] >= H[K]) {
son = 0;
}
}
if (son) {
swap(H[K], H[son]);
K = son;
}
} while (son);
}
//Urcam nodul K in heap pana ajunge pe o pozitie favorabila
void percolate(int K){
int key = H[K];
while(K > 1 && (key < H[father(K)])){
H[K] = H[father(K)];
K = father(K);
}
H[K] = key;
}
void insert(int val){
H[++N] = val;
percolate(N);
}
void del(int K){
H[K] = H[N];
--N;
if((K > 1) &&(H[K] < H[father(K)])){
percolate(K);
}else{
sift(K);
}
}
int minim_heap(){
return H[1];
}
int main(){
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int n;
fin>>n;
int i, x;
for(i = 1 ; i <= n ; i++){
fin>>x;
insert(x);
}
while(N){
fout<<minim_heap()<< " ";
del(1);
}
return 0;
}