Pagini recente » Cod sursa (job #339582) | Cod sursa (job #2449002) | Cod sursa (job #149511) | Cod sursa (job #3292827) | Cod sursa (job #969603)
Cod sursa(job #969603)
#include <fstream>
#define nmax 500500
using namespace std;
int N;
class heap {
#define father(node) (node>>1)
#define leftSon(node) (node<<1)
#define rightSon(node) ((node<<1)|1)
#define compare(a, b) ( H[a] < H[b] )
#define compareMin(a, b) compare(a, b)
#define comapreMax(a, b) compare(b, a)
public:
int Top, H[nmax], Index[nmax];
public:
heap() {
Top = 0;
}
void precolate(int Node) {
while( Node > 1 && compareMin(Node, father(Node)) ) {
swap(H[Node], H[father(Node)]);
Node = father(Node);
}
}
void sift(int Node) {
int Son;
do {
Son = 0;
if( leftSon(Node) <= this -> size() ) {
Son = leftSon(Node);
if( rightSon(Node) <= this -> size() && compareMin(rightSon(Node), Son) )
Son = rightSon(Node);
if( compareMin(Node, Son) )
Son = 0;
}
if( Son ) {
swap(H[Node], H[Son]);
Node = Son;
}
} while( Son );
}
void push(int Value) {
H[ ++Top ] = Value;
this -> precolate(Top);
}
void pop() {
H[1] = H[ Top-- ];
this -> sift(1);
}
int size() {
return Top;
}
bool empty() {
return ( Top == 0);
}
int top() {
return H[1];
}
};
heap Heap;
void Read() {
int X;
ifstream in("algsort.in");
in >> N;
for(int i = 1; i <= N; i++) {
in >> X;
Heap.push(X);
}
in.close();
}
void Write() {
ofstream out("algsort.out");
for(int i = 1; i <= N; i++) {
out << Heap.top() << ' ';
Heap.pop();
}
out << '\n';
out.close();
}
int main() {
Read();
Write();
return 0;
}