Heap Sort Algorithm

An efficient comparison-based sorting algorithm that uses a binary heap data structure, achieving O(n log n) time complexity.

Learning Heap Sort

Heap Sort is an efficient comparison-based sorting algorithm that uses a specialized tree-based data structure called a binary heap. It combines the best attributes of insertion sort (good for small data sets) and merge sort (good for large data sets) by using the heap data structure.

How Heap Sort Works

Heap Sort consists of two main phases:

  1. Build a Max Heap: Convert the input array into a complete binary tree that satisfies the max heap property (where each parent node is greater than or equal to its children).
  2. Extract Elements: Repeatedly extract the maximum element from the heap (which is always at the root) and place it at the end of the array, reducing the heap size each time.

The Heap Data Structure

A binary heap is a complete binary tree where:

  • All levels are completely filled except possibly the last level
  • The last level is filled from left to right
  • In a max heap, each parent node is greater than or equal to its children
  • In a min heap, each parent node is less than or equal to its children

Heaps can be efficiently represented as arrays, where for any element at index i:

  • The parent element is at index ⌊(i-1)/2⌋
  • The left child is at index 2i + 1
  • The right child is at index 2i + 2

The Algorithm Steps

  1. Build a max heap from the input data.
    • Start from the first non-leaf node (at index n/2-1, where n is array length) and heapify each subtree from bottom up.
    • This ensures that the largest element is at the root of the heap.
  2. Repeatedly extract the maximum element:
    • Swap the root (maximum element) with the last element in the heap.
    • Reduce the heap size by one.
    • Heapify the root element to maintain the max heap property with the reduced heap.
    • Continue this process until the heap is empty.

The Heapify Process

Heapify is the key operation in Heap Sort. It works as follows:

  1. Start with a node at index i.
  2. Find the largest among the node and its children (at 2i+1 and 2i+2).
  3. If the largest element is not the parent, swap it with the parent.
  4. Continue this process recursively for the affected subtree.

Time and Space Complexity

  • Time Complexity:
    • Best Case: O(n log n)
    • Average Case: O(n log n)
    • Worst Case: O(n log n)
    • Building the initial heap takes O(n) time
    • Extracting n elements takes O(n log n) time
  • Space Complexity: O(1) as it sorts in-place

Key Characteristics

  • In-place sorting: Only requires a constant amount O(1) of additional memory
  • Not stable: Does not preserve the relative order of equal elements
  • Efficient: Guaranteed O(n log n) time complexity in all cases
  • Not adaptive: Performance is not significantly affected by the initial order of the data
  • Internal algorithm: All data to be sorted must be in memory

Visualizing the Process

The Heap Sort visualization in this tool:

  1. Shows the array represented as a binary tree to clearly depict the heap structure
  2. Highlights parent-child relationships during comparisons
  3. Animates the swapping process during heapify operations
  4. Shows how elements are extracted to build the sorted array
  5. Uses different colors to indicate different states of elements (comparing, swapping, sorted)

Real-world Applications

  • Operating system job scheduling algorithms
  • Priority queues in graph algorithms like Dijkstra's shortest path
  • In-place sorting of large datasets where space is a constraint
  • Systems where guaranteed worst-case performance is critical