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.
- Reduce heap size by 1.
- Heapify the root to restore heap property.
- The array is now sorted in ascending order.
How Heap Sort Works
Step 1: Represent the Array as a Binary Tree
For an array of size n,
- Root is at index 0
- Left child of node i: 2*i + 1
- Right child of node i: 2*i + 2
Array as Binary TreeStep 2: Build a Max Heap
Convert the array into a max heap where each parent is larger than its children.
Step 3: Sort the array by placing largest element at end of unsorted array.
Below are the detailed steps to sort the array:
- Swap the root (maximum) with the last element.
- Reduce heap size and heapify the root again.
- Repeat until the heap has one element left.
Python Implementation
Python
def heapify(arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and arr[l] > arr[largest]:
largest = l
if r < n and arr[r] > arr[largest]:
largest = r
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
def heapSort(arr):
n = len(arr)
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)
for i in range(n - 1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # Swap max to end
heapify(arr, i, 0)
arr = [12, 11, 13, 5, 6, 7]
heapSort(arr)
print("Sorted array is:", arr)
Output('Sorted array is:', [5, 6, 7, 11, 12, 13])
Explanation:
Build Max Heap
- The loop for i in range(n // 2 - 1, -1, -1) builds a Max Heap from the array.
- Starts from the last non-leaf node and moves upward, calling heapify() for each node.
- Ensures every parent node is greater than its children, making the largest element the root of the heap.
- The loop for i in range(n - 1, 0, -1) extracts the maximum element (root) one by one.
- Swaps it with the last element and reduces heap size by one.
- Reduces heap size by one and calls heapify() on the new root to restore heap structure.
Heapify
- The heapify() function ensures that the subtree with root index i maintains the Max Heap property.
- Compares the node with its left and right children (l = 2*i + 1, r = 2*i + 2).
- If a child is larger, swaps it with the parent and recursively heapifies the affected subtree.
Related Articles:
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem