Heap Sort – Python
Heapsort is a comparison-based sorting technique based on a Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for the remaining element.
Heap Sort Algorithm
First convert the array into a max heap using heapify, Please note that this happens in-place. The array elements are re-arranged to follow heap properties. Then one by one delete the root node of the Max-heap and replace it with the last node and heapify. Repeat this process while size of heap is greater than 1.
- Rearrange array elements so that they form a Max Heap.
- Repeat the following steps until the heap contains only one element:
- Swap the root element of the heap (which is the largest element in current heap) with the last element of the heap.
- Remove the last element of the heap (which is now in the correct position). We mainly reduce heap size and do not remove element from the actual array.
- Heapify the remaining elements of the heap.
- Finally we get sorted array.
Working of Heap Sort
Step 1: Treat the Array as a Complete Binary Tree
We first need to visualize the array as a complete binary tree. For an array of size n, the root is at index 0, the left child of an element at index i is at 2i + 1, and the right child is at 2i + 2.

Array as Binary Tree
Step 2: Build a Max Heap
Below are the detailed steps to heapify the tree:














Step 3: Sort the array by placing largest element at end of unsorted array.
Below are the detailed steps to sort the array:












In the illustration above, we have shown some steps to sort the array. We need to keep repeating these steps until there’s only one element left in the heap.
The given Python code implements the Heap Sort algorithm, which is an efficient comparison-based sorting method.
def heapify(arr, n, i):
largest = i # Initialize largest as root
l = 2 * i + 1 # left = 2*i + 1
r = 2 * i + 2 # right = 2*i + 2
# See if left child of root exists and is
# greater than root
if l < n and arr[i] < arr[l]:
largest = l
# See if right child of root exists and is
# greater than root
if r < n and arr[largest] < arr[r]:
largest = r
# Change root, if needed
if largest != i:
(arr[i], arr[largest]) = (arr[largest], arr[i]) # swap
# Heapify the root.
heapify(arr, n, largest)
# The main function to sort an array of given size
def heapSort(arr):
n = len(arr)
# Build a maxheap.
# Since last parent will be at (n//2) we can start at that location.
for i in range(n // 2, -1, -1):
heapify(arr, n, i)
# One by one extract elements
for i in range(n - 1, 0, -1):
(arr[i], arr[0]) = (arr[0], arr[i]) # swap
heapify(arr, i, 0)
# Driver code to test above
arr = [12, 11, 13, 5, 6, 7, ]
heapSort(arr)
n = len(arr)
print('Sorted array is')
for i in range(n):
print(arr[i])
Output
Sorted array is 5 6 7 11 12 13
Time Complexity: O(n*log(n))
- The time complexity of heapify is O(log(n)).
- Time complexity of createAndBuildHeap() is O(n).
- And, hence the overall time complexity of Heap Sort is O(n*log(n)).
Auxiliary Space: O(log(n))
Using Python STL
Steps:
- Import the Python STL library “heapq“.
- Convert the input list into a heap using the “heapify” function from heapq.
- Create an empty list “result” to store the sorted elements.
- Iterate over the heap and extract the minimum element using “heappop” function from heapq and append it to the “result” list.
- Return the “result” list as the sorted output.
import heapq
# Function to perform the sorting using
# heaop sort
def heap_sort(arr):
heapq.heapify(arr)
result = []
while arr:
result.append(heapq.heappop(arr))
return result
# Driver Code
arr = [60, 20, 40, 70, 30, 10]
print("Input Array: ", arr)
print("Sorted Array: ", heap_sort(arr))
Output
Input Array: [60, 20, 40, 70, 30, 10] Sorted Array: [10, 20, 30, 40, 60, 70]
Time Complexity: O(n log n), where “n” is the size of the input list.
Auxiliary Space: O(1).
Please refer complete article on Heap Sort for more details!