const arr = [20,3,11,4,15,8,7,6,9,0];
function swap(array, indexA, indexB) {
const temp = array[indexA];
array[indexA] = array[indexB];
array[indexB] = temp;
}
function maxHeapify(array, index, heapSize) {
let max = index;
const left = index * 2 + 1,
right = index*2 + 2;
if (left <= heapSize && array[left] > array[max]) {
max = left;
}
if (right <= heapSize && array[right] > array[max]) {
max = right;
}
if (max !== index) {
swap(array, index, max);
maxHeapify(array, max, heapSize);
}
}
function buildHeap(array) {
const lastNotLeaf = Math.floor((array.length -1) / 2);
for (let i = lastNotLeaf; i >= 0; i-- ) {
maxHeapify(array, i, array.length-1);
}
}
function heapSort(array) {
buildHeap(array);
for (let i = array.length-1; i >= 0; i--) {
swap(array, 0, i);
maxHeapify(array, 0, i-1);
}
}
heapSort(arr)
console.log(arr);
console