Heap Sort is a comparison-based sorting algorithm that uses a Binary Heap data structure. It works similarly to Selection Sort, where we repeatedly extract the maximum element and move it to the end of the array.
Heap Sort Algorithm
Heap Sort works in two main phases:
Build a Max Heap from the input data.
Extract elements from the heap one by one, placing the maximum element at the end each time.
Algorithm Steps:
Rearrange array elements to form a Max Heap.
Repeat until the heap has only one element:
Swap the root (maximum element) with the last element of the heap.