Heap Sort

The process begins by comparing the two child nodes on the last level and then swapping the appropriate one with their parent. This procedure starts from the far right of the final level and ascends in reverse level order.

From the second-to-last level upwards, once a swap is made, the process continues downward, performing swaps as it descends. This operation has a time complexity of O(nlogn)O(nlogn) and falls under the category of in-place sorting methods.

BUILD-MAX-HEAP(A)
for i = A.length downto 2
    excahnge A[1] with A[i]
    A.heap-size = A.heap-size - 1
    MAX-HEAPIFY(A, 1)

Visualization: https://www.cs.usfca.edu/~galles/visualization/HeapSort.html

Last updated