I’m working on my next tutorial that describes how heapsort works, but who knows when I’ll actually finish it! In the meantime, here is my heapsort implementation in JavaScript:

```
var arrayLength;
function buildHeap(input) {
arrayLength = input.length;
for (var i = Math.floor(arrayLength / 2); i >= 0; i -= 1) {
heapify(input, i);
}
}
function heapify(input, i) {
var left = 2 * i + 1;
var right = 2 * i + 2;
var largest = i;
if (left < arrayLength && input[left] > input[largest]) {
largest = left;
}
if (right < arrayLength && input[right] > input[largest]) {
largest = right;
}
if (largest != i) {
swap(input, i, largest);
heapify(input, largest);
}
}
function swap(input, index_A, index_B) {
var temp = input[index_A];
input[index_A] = input[index_B];
input[index_B] = temp;
}
function heapSort(input) {
buildHeap(input);
for (var i = input.length - 1; i > 0; i--) {
swap(input, 0, i);
arrayLength--;
heapify(input, 0);
}
}
var example = [40, 10, 50, 24, 1, 2, 4, -10, 15, 7, 8, 5];
heapSort(example);
console.log(example); // -10,1,2,4,5,7,8,10,15,24,40,50
```

Hopefully this is useful for somebody

Cheers,

Kirupa