Skip to content

Instantly share code, notes, and snippets.

@vaidehijoshi
Created July 13, 2017 14:57
Show Gist options
  • Save vaidehijoshi/2c8ee361b8a37f13ebbf8e7eb824d9dc to your computer and use it in GitHub Desktop.
Save vaidehijoshi/2c8ee361b8a37f13ebbf8e7eb824d9dc to your computer and use it in GitHub Desktop.
function heapify(heap, i, max) {
var index, leftChild, righChild;
while(i < max) {
index = i;
leftChild = 2*i + 1;
righChild = leftChild + 1;
if (leftChild < max && heap[leftChild] > heap[index]) {
index = leftChild;
}
if (righChild < max && heap[righChild] > heap[index]) {
index = righChild;
}
if (index == i) {
return;
}
swap(heap,i, index);
i = index;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment