Heap Sort is a comparison-based sorting algorithm that leverages a binary heap data structure to sort elements efficiently. The algorithm works by first building a heap (either a max-heap or a min-heap) from the input data, and then repeatedly extracting the maximum (for max-heap) or minimum (for min-heap) element from the heap, rebuilding the heap after each extraction.
Build a Max-Heap from the input array:
Swap the root (maximum element) with the last element in the heap.
Reduce the size of the heap by 1 (exclude the last element, which is now sorted).
Repeat the process until the heap size becomes 1. By then, the array will be sorted in ascending order.
Building the Max-Heap:
n/2 - 1), we call heapify on each node in reverse order until we reach the root.Extracting the Maximum Element:
Repeat Until Sorted:
Building the Heap:
n/2 - 1 down to the root), which takes O(log n) time. The total cost is dominated by the work on the lower levels of the tree, which is linear in total.Extracting the Maximum Element:
n times (one for each element), the time complexity for the extraction phase is O(n log n).Total Time Complexity:
The overall time complexity of the heap sort algorithm is dominated by the O(n log n) extraction phase, while the heap building phase takes O(n) time. Therefore, the total time complexity of heap sort is O(n log n).
Best-case Time Complexity: O(n log n)
Average-case Time Complexity: O(n log n)
Worst-case Time Complexity: O(n log n)
Heap sort has the same time complexity in all cases because it always performs O(log n) operations for heapifying after each extraction, regardless of the input data.
Here is the pseudocode for heap sort:
def heapify(arr, n, i):
largest = i # Initialize largest as root
left = 2 * i + 1 # Left child
right = 2 * i + 2 # Right child
# Check if left child exists and is greater than root
if left < n and arr[largest] < arr[left]:
largest = left
# Check if right child exists and is greater than root
if right < n and arr[largest] < arr[right]:
largest = right
# Change root, if needed
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i] # Swap
heapify(arr, n, largest) # Recursively heapify the affected sub-tree
def heapSort(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 from the heap
for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # Swap root (max) with the last element
heapify(arr, i, 0) # Heapify the reduced heap
return arr
Let's sort an array using heap sort:
Input: [4, 10, 3, 5, 1]
Build the Max-Heap:
10
/ \
5 3
/ \
4 1
First Extraction (Swap root with last element):
10 with 1, resulting in the array [1, 5, 3, 4, 10].5, so swap 1 and 5.[5, 4, 3, 1, 10].Second Extraction (Swap root with second-last element):
5 with 1, resulting in [1, 4, 3, 5, 10].[4, 1, 3, 5, 10].Repeat the extraction process for the remaining elements:
4 with 1, resulting in [1, 4, 3].Final sorted array: [1, 3, 4, 5, 10].
Time Complexity:
Heap sort guarantees O(n log n) time complexity in the worst, average, and best cases. This makes it a reliable sorting algorithm.
In-place Sorting:
Heap sort is an in-place algorithm, meaning it does not require additional space apart from a small constant amount for temporary variables.
Non-Recursive Version:
Although the recursive heapify method can be used, heap sort can also be implemented non-recursively, which may be advantageous in some scenarios.
Not Stable:
Heap sort is not a stable sort (i.e., equal elements may not maintain their original relative order).
Not Adaptive:
Unlike algorithms like Insertion Sort, heap sort does not adapt to partially sorted input arrays. It always runs in O(n log n) time.
Constant Factors:
While the time complexity is good, the constant factors involved in heap sort may make it slower in practice than algorithms like Merge Sort or Quick Sort for smaller datasets.
Heap sort is a comparison-based sorting algorithm with O(n log n) time complexity in all cases. It has the advantage of being in-place and guarantees good performance even in the worst case. However, it is not stable and may have slightly worse practical performance due to larger constant factors compared to other algorithms like Quick Sort or Merge Sort.
Open this section to load past papers