var Heap = []
var sizeH = 0
function up( child ) {
var parent = parseInt(child / 2)
while( parent ) {
if(Heap[parent] > Heap[child]) {
var aux = Heap[parent]
Heap[parent] = Heap[child]
Heap[child] = aux
child = parent
parent = parseInt(child / 2)
} else {
break
}
}
}
function down( parent ) {
while( 2 * parent <= sizeH ) {
var child = 2 * parent
if( (2 * parent + 1) <= sizeH && Heap[ 2 * parent + 1 ] < Heap[ 2 * parent ]) {
child += 1
}
if(Heap[parent] <= Heap[child]) {
break
}
var aux = Heap[parent]
Heap[parent] = Heap[child]
Heap[child] = aux
parent = child
}
}
function insertHeap( value ) {
Heap[ ++sizeH ] = value
up( sizeH )
}
function removeHeap( ) {
ret = Heap[ 1 ]
Heap[ 1 ] = Heap[ sizeH ]
sizeH -= 1
down( 1 )
return ret
}
function heapify( arr ) {
n = arr.length
for (var i = 0; i < n; i++) {
insertHeap( arr[i] )
}
}
arr = [9, 8, 7, 6, -5, -1, -3, 5]
n = arr.length
out = []
heapify( arr )
for (var i = 0; i < n; i++) {
out.push( removeHeap() )
}
print("Input:")
for(var i in arr) {
print(arr[i])
}
print("Output:")
for(var i in out) {
print(out[i])
}
dmFyIEhlYXAgPSBbXQp2YXIgc2l6ZUggPSAwCgpmdW5jdGlvbiB1cCggY2hpbGQgKSB7CgogICAgICB2YXIgcGFyZW50ID0gcGFyc2VJbnQoY2hpbGQgLyAyKQoKICAgICAgd2hpbGUoIHBhcmVudCApIHsKCiAgICAgICAgICAgIGlmKEhlYXBbcGFyZW50XSA+IEhlYXBbY2hpbGRdKSB7CgogICAgICAgICAgICAgICB2YXIgYXV4ID0gSGVhcFtwYXJlbnRdCiAgICAgICAgICAgICAgICAgICBIZWFwW3BhcmVudF0gPSBIZWFwW2NoaWxkXQogICAgICAgICAgICAgICAgICAgSGVhcFtjaGlsZF0gPSBhdXgKCiAgICAgICAgICAgICAgICAgICBjaGlsZCA9IHBhcmVudAogICAgICAgICAgICAgICAgICAgcGFyZW50ID0gcGFyc2VJbnQoY2hpbGQgLyAyKQogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIGJyZWFrCiAgICAgICAgICAgIH0KICAgICAgfQp9CgpmdW5jdGlvbiBkb3duKCBwYXJlbnQgKSB7CgogICAgICAgICB3aGlsZSggMiAqIHBhcmVudCA8PSBzaXplSCApIHsKCiAgICAgICAgICAgICAgICB2YXIgY2hpbGQgPSAyICogcGFyZW50CgogICAgICAgICAgICAgICAgaWYoICgyICogcGFyZW50ICsgMSkgPD0gc2l6ZUggJiYgSGVhcFsgMiAqIHBhcmVudCArIDEgXSA8IEhlYXBbIDIgKiBwYXJlbnQgXSkgewoKICAgICAgICAgICAgICAgICAgICBjaGlsZCArPSAxCiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgaWYoSGVhcFtwYXJlbnRdIDw9IEhlYXBbY2hpbGRdKSB7CiAgICAgICAgICAgICAgICAgIGJyZWFrCiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgdmFyIGF1eCA9IEhlYXBbcGFyZW50XQogICAgICAgICAgICAgICAgICAgIEhlYXBbcGFyZW50XSA9IEhlYXBbY2hpbGRdCiAgICAgICAgICAgICAgICAgICAgSGVhcFtjaGlsZF0gPSBhdXgKCiAgICAgICAgICAgICAgICAgcGFyZW50ID0gY2hpbGQKICAgICAgICAgfQp9CgpmdW5jdGlvbiBpbnNlcnRIZWFwKCB2YWx1ZSApIHsKCiAgICAgIEhlYXBbICsrc2l6ZUggXSA9IHZhbHVlCgogICAgICB1cCggc2l6ZUggKQp9CgpmdW5jdGlvbiByZW1vdmVIZWFwKCApIHsKCiAgICAgICByZXQgPSBIZWFwWyAxIF0KCiAgICAgICBIZWFwWyAxIF0gPSBIZWFwWyBzaXplSCBdCgogICAgICAgc2l6ZUggLT0gMQoKICAgICAgIGRvd24oIDEgKQoKICAgICAgIHJldHVybiByZXQKCn0KCmZ1bmN0aW9uIGhlYXBpZnkoIGFyciApIHsKCiAgICAgICAgbiA9IGFyci5sZW5ndGgKCiAgICAgICAgZm9yICh2YXIgaSA9IDA7IGkgPCBuOyBpKyspIHsKCiAgICAgICAgICAgICBpbnNlcnRIZWFwKCBhcnJbaV0gKQogICAgICAgIH0KfQoKYXJyID0gWzksIDgsIDcsIDYsIC01LCAtMSwgLTMsIDVdCgpuID0gYXJyLmxlbmd0aAoKb3V0ID0gW10KCmhlYXBpZnkoIGFyciApCgpmb3IgKHZhciBpID0gMDsgaSA8IG47IGkrKykgewoKICAgIG91dC5wdXNoKCByZW1vdmVIZWFwKCkgKQp9CgpwcmludCgiSW5wdXQ6IikKZm9yKHZhciBpIGluIGFycikgewoJcHJpbnQoYXJyW2ldKQp9CgoKcHJpbnQoIk91dHB1dDoiKQpmb3IodmFyIGkgaW4gb3V0KSB7CglwcmludChvdXRbaV0pCn0KCg==