Heap Sort Python (original) (raw)

Last Updated : 16 Jan, 2026

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:

  1. Build a Max Heap from the input data.
  2. Extract elements from the heap one by one, placing the maximum element at the end each time.

Algorithm Steps:

  1. Rearrange array elements to form a Max Heap.
  2. Repeat until the heap has only one element:
  3. Swap the root (maximum element) with the last element of the heap.
  4. Reduce heap size by 1.
  5. Heapify the root to restore heap property.
  6. 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,

Visualize-the-array-as-a-complete-binary-tree

Array as Binary Tree

Step 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:

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

Heapify