Cod sursa(job #1850529)

Utilizator NarniussAnghelache Bogdan Narniuss Data 18 ianuarie 2017 18:45:21
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#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);
}


int minim_heap(Heap H){
  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);
  }

  for(i = 1 ; i <= N ; i++)
    fout<<H[i]<< " ";


  return 0;
}