Heap Sort

Max heap

// An array of objects with a key property must be passed. 
// eg- {key: 8, value: "n", ...}

var arr = [
    { key: 4, data: "a" }, 
    { key: 1, data: "b" }, 
    { key: 3, data: "c" }, 
    { key: 5, data: "d" }, 
    { key: 2, data: "e" }
]

// Call heapsort on the array
// Creates a max heap i.e. sorts in accending order
HeapSort(arr)

// Sorts the array in place
console.log(arr);

Min heap

// An array of objects with a key property must be passed. 
// eg- {key: 8, value: "n", ...}

var arr = [
    { key: 4, data: "a" }, 
    { key: 1, data: "b" }, 
    { key: 3, data: "c" }, 
    { key: 5, data: "d" }, 
    { key: 2, data: "e" }
]

// To use a min heap i.e. sorting in decending order
// Pass true as the second parameter
HeapSort(arr, true)

// Sorts the array in place
console.log(arr);