Pagini recente » Cod sursa (job #3150283) | Rating Pietrareanu George (George26) | Cod sursa (job #2262830) | Cod sursa (job #2450740) | Cod sursa (job #1232440)
#include <fstream>;
using namespace std;
ifstream inFile("algsort.in");
ofstream outFile("algsort.out");
class MIN_HEAP{
public:
int H[500005];
int N;
void create(){ N = 0; }
bool isEmpty(){ return N == 0; }
int get_min(){ return H[1]; }
int father(int nod){ return nod/2; }
int left_son(int nod){ return 2*nod; }
int right_son(int nod){ return 2*nod+1; }
void coboara(int nod){
if( left_son(nod) > N ) return;
if( right_son(nod) > N ){
if( H[nod] > H[left_son(nod)] ) swap( H[nod], H[left_son(nod)] );
return;
}
int next_nod = H[left_son(nod) ] < H[right_son(nod)] ? left_son(nod) : right_son(nod);
if( H[nod] > H[next_nod] ){
swap(H[nod], H[next_nod]);
coboara(next_nod);
}
}
void urca(int nod){
if( nod == 1 ) return;
if( H[father(nod)] > H[nod] ){
swap( H[father(nod)], H[nod] );
urca(father(nod));
}
}
void insert(int val){
if( N == 0 ) H[++N] = val;
else{
N++;
H[N] = val;
urca(N);
}
}
void del(int nod){
if( nod == N) N--;
else{
swap(H[N], H[nod]);
N--;
coboara(nod);
}
}
void print(){
for(int i=1; i<=N; i++) outFile << H[i] << " ";
}
};
int main()
{
int n;
inFile >> n;
MIN_HEAP H;
H.create();
int x;
for(int i = 1; i <= n; i++){
inFile >> x;
H.insert(x);
}
while(n){
outFile << H.get_min() << " ";
H.del(1);
n--;
}
}