Heap Sort Algorithm
What is a Heap?
A heap is a specialized tree-based data structure that satisfies the heap property. The heap property depends on whether it's a max-heap or a min-heap:
-
Max-Heap Property: For every node i other than the root, the value of i is less than or equal to the value of its parent.
-
Min-Heap Property: For every node i other than the root, the value of i is greater than or equal to the value of its parent.
In the context of heap sort, we typically use a max-heap.
What is Heap Sort?
Heap Sort is a comparison-based sorting algorithm that uses a binary heap data structure to build a partially ordered tree and efficiently find the maximum (for max-heap) element repeatedly. The algorithm proceeds by building the heap, extracting the maximum element (root), and then restoring the heap property.
Working of Heap Sort Algorithm
-
Build Max-Heap:
- Convert the input array into a max-heap. This is done by starting from the last non-leaf node and repeatedly heapifying downward.
-
Extract Maximum:
- Swap the root (maximum element) with the last element in the heap and reduce the size of the heap.
-
Heapify:
- Heapify the root to maintain the max-heap property.
-
Repeat:
- Repeat steps 2 and 3 until the heap size is 1.
Let's delve into the mechanics of the Heapsort Algorithm.
Heapsort entails two key phases in its element sorting process:
- First and foremost, a heap is fashioned by adjusting the array elements.
- Subsequently, the root element of the heap is systematically removed, shifting it to the array's end. The remaining elements maintain the heap structure.
To illustrate the intricacies of heapsort, consider an unsorted array. Through a detailed example, we can gain a clearer understanding of the algorithm's operations and how it efficiently transforms an unordered array into a sorted one.

First, we have to construct a heap from the given array and convert it into max heap.

After converting the given heap into max heap, the array elements are -

Next, we have to delete the root element (89) from the max heap. To delete this node, we have to swap it with the last node, i.e. (11). After deleting the root element, we again have to heapify it to convert it into max heap.

After swapping the array element 89 with 11, and converting the heap into max-heap, the elements of array are -

In the next step, again, we have to delete the root element (81) from the max heap. To delete this node, we have to swap it with the last node, i.e. (54). After deleting the root element, we again have to heapify it to convert it into max heap.

After swapping the array element 81 with 54 and converting the heap into max-heap, the elements of array are -

In the next step, we have to delete the root element (76) from the max heap again. To delete this node, we have to swap it with the last node, i.e. (9). After deleting the root element, we again have to heapify it to convert it into max heap.

After swapping the array element 76 with 9 and converting the heap into max-heap, the elements of array are -

In the next step, again we have to delete the root element (54) from the max heap. To delete this node, we have to swap it with the last node, i.e. (14). After deleting the root element, we again have to heapify it to convert it into max heap.

After swapping the array element 54 with 14 and converting the heap into max-heap, the elements of array are -

In the next step, again we have to delete the root element (22) from the max heap. To delete this node, we have to swap it with the last node, i.e. (11). After deleting the root element, we again have to heapify it to convert it into max heap.

After swapping the array element 22 with 11 and converting the heap into max-heap, the elements of array are -

In the next step, again we have to delete the root element (14) from the max heap. To delete this node, we have to swap it with the last node, i.e. (9). After deleting the root element, we again have to heapify it to convert it into max heap.

After swapping the array element 14 with 9 and converting the heap into max-heap, the elements of array are -

In the next step, again we have to delete the root element (11) from the max heap. To delete this node, we have to swap it with the last node, i.e. (9). After deleting the root element, we again have to heapify it to convert it into max heap.

After swapping the array element 11 with 9, the elements of array are -

Now, heap has only one element left. After deleting it, heap will be empty.

After completion of sorting, the array elements are -

Now, the array is completely sorted.
Heap Sort Complexity
-
Time Complexity:
- Build Max-Heap: O(n)
- Extract Maximum (n times): O(n log n)
- Overall Time Complexity: O(n log n)
-
Space Complexity:
- O(1) - Heap sort is an in-place sorting algorithm.
Implementation of Heap Sort
Below is a simple implementation of the Heap Sort algorithm in Python:
def heapify(arr, n, i):
largest = i # Initialize the largest as the root
left_child = 2 * i + 1
right_child = 2 * i + 2
# Check if left child exists and is greater than the root
if left_child < n and arr[left_child] > arr[largest]:
largest = left_child
# Check if right child exists and is greater than the root
if right_child < n and arr[right_child] > arr[largest]:
largest = right_child
# Swap the root if needed
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
# Heapify the affected sub-tree
heapify(arr, n, largest)
def heap_sort(arr):
n = len(arr)
# Build a max heap
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)
# Extract elements one by one
for i in range(n - 1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # Swap
heapify(arr, i, 0)
# Example Usage
arr = [12, 11, 13, 5, 6, 7]
heap_sort(arr)
print("Sorted array:", arr)
In this example, heapify is a function to heapify a subtree rooted at index i, and heap_sort uses this function to perform the sorting.
This implementation assumes that the input is a list of comparable elements. The heapify function is a crucial part of the algorithm as it ensures that the max-heap property is maintained during the extraction of the maximum element and heapification processes.