Description

Heap Sort is a comparison-based sorting algorithm that utilizes a heap data structure to organize elements. It works by creating a heap from unsorted data and repeatedly extracting elements from the heap in sorted order.

The algorithm consists of two main phases:

  1. Building the Heap: Converting the input array into a heap.
  2. Extracting Elements from the Heap: Extracting the root (the maximum or minimum element depending on the type of heap), swapping it with the last element in the heap, and restoring the heap property for the remaining elements.

The Heap Sort algorithm is unstable, as duplicate elements may change their relative order during execution.

How It Works

  1. A heap (max-heap or min-heap) is built, organizing elements such that the maximum (or minimum) element is always at the top (root).
  2. The maximum (or minimum) element is extracted and moved to the end of the array.
  3. The heap structure is restored for the remaining elements.
  4. This process repeats until all elements are sorted.

Complexity

  • Time Complexity:
  • Building the heap: O(n)
  • Extracting an element & restoring the heap: O(log n)
  • Total Complexity: O(n log n) across best, average, and worst cases.
  • Space Complexity: O(1) — sorting is performed in-place without additional memory allocations beyond the array itself.

Steps of the Heap Sort Algorithm

  1. Build the Heap: Transform the array into a heap. Start from the last internal (non-leaf) node and move upward to the root, calling the heapify operation on each node.
  2. Extract Elements: Swap the root element (maximum or minimum) with the last element of the heap. Reduce the heap size by 1 and call heapify on the root to restore heap properties.

Code Explanation

  1. In a HeapSort implementation, the primary methods are:
  2. heapify(n, i): Restores the heap property for a subtree rooted at index i, where n is the current size of the heap. Checks i against its left and right children and swaps if a child is larger (in a max-heap).
  3. sort(arr): Sorts the array by first building the heap and then repeatedly extracting elements while restoring the heap at each step.
  4. Inside the sort method:
  5. Heap construction starts from the last parent node at index n // 2 - 1 down to 0.
  6. Once constructed, elements are extracted sequentially by swapping the root to the end of the active array range and re-heapifying.

Advantages of Heap Sort

  • Guaranteed Time Complexity: Guarantees O(n log n) runtime performance in the worst, best, and average cases.
  • Low Memory Footprint: Performs sorting "in-place" using constant extra space, O(1).

Disadvantages of Heap Sort

  • Unstable Sort: Relative order of identical elements is not preserved.
  • Implementation Overhead: Requires multiple auxiliary operations to rebuild the heap, making it slightly more complex than algorithms like Quick Sort or Insertion Sort for small datasets.

Heap Sort is particularly useful when guaranteed O(n log n) runtime is required regardless of the initial ordering of elements, and when minimal memory usage is critical.