Given an array of integers nums, sort the array in ascending order using the Heap Sort algorithm — first build a max-heap from the array (stored implicitly by index, no extra tree structure needed), then repeatedly swap the root (the current maximum) to the end of the unsorted region and re-heapify the shrinking heap.
nums = [8,4,3,7,6][3,4,6,7,8]Max-heap invariant: every parent is ≥ both its children, so index 0 is always the maximum of the current heap. Building the heap bottom-up (sift-down from the last non-leaf node down to the root) establishes this invariant in O(n). Each extraction swaps the known-maximum root to the end of the unsorted region — placing it in its final sorted position — then shrinks the heap and sifts the new root down in O(log n) to restore the invariant. Repeating this n-1 times yields an ascending sorted array, entirely in-place.