What is Heap Sort in Python?

PythonServer Side ProgrammingProgramming

Heap Sort is the sorting technique based on binary heap data structure. In order to proceed with heap sort, you need to be familiar with binary tree and binary heap.

What is a Complete binary tree?

A complete binary tree is a tree data structure where all the levels except the last one are completely filled. The last level must be filled from the left side.

What is a Binary heap?

Binary heap is a special case of the binary tree. Binary heaps are of two types −

  • Max Heap − The parent node at each level is greater than its child nodes.

  • Min Heap − The parent node at each level is smaller than its child nodes.

Array Representation of complete binary tree

The binary heap can be represented as an array since it is space efficient. If the parent node is stored at index I, the left child can be calculated by 2 * i + 1 and right child by 2 *i + 2. Assuming, the index starts from 0.

Heap Sort Algorithm

  • Build the max heap from a complete binary tree.

  • Remove the root and replace it with the last element in the heap, reduce the size of the heap by 1and again build a max heap from the remaining nodes.

  • Repeat step 2 until we have only 1 node left.

Build max heap from a complete binary tree

This is the code for building max heap from a complete binary tree, where the two child nodes are compared with the root. If the Larger element is not the root, then swap the larger element with the root. This is a recursive procedure. The current root which is smaller than its child nodes is continuously compared to its lower subtrees unless it reaches its correct position.

The following code builds a max heap from a complete binary tree which is basically the array which we want to sort.

def heapify(arr, n, i):
   # Find largest among root and children
   largest = i
   l = 2 * i + 1
   r = 2 * i + 2
   if l < n and arr[i] < arr[l]:
      largest = l
   if r < n and arr[largest] < arr[r]:
      largest = r
   # If root is not largest, swap with largest and continue heapifying
   if largest != i:
      arr[i], arr[largest] = arr[largest], arr[i]
      heapify(arr, n, largest)

Heap Sort

At this time, we have the max heap with us. Now we need to do the following things.

  • Swap the root with the last element in the heap.

  • Reduce the size of heap by 1.(This means that the largest element has reached at the last position, we don’t need to take that element into consideration).

  • Rebuild max heap excluding the last element.

  • Repeat the above steps until we have only 1 element left.

for i in range(n-1, 0, -1):
   # Swap
   arr[i], arr[0] = arr[0], arr[i]

   # Heapify root element
   heapify(arr, i, 0)

The complete program for heap sort in Python is as follows −

def heapify(arr, n, i):
   # Find largest among root and children
   largest = i
   l = 2 * i + 1
   r = 2 * i + 2
   if l < n and arr[i] < arr[l]:
      largest = l
   if r < n and arr[largest] < arr[r]:
      largest = r
   # If root is not largest, swap with largest and continue heapifying
   if largest != i:
      arr[i], arr[largest] = arr[largest], arr[i]
      heapify(arr, n, largest)

def heapSort(arr):
   n = len(arr)
   # Build max heap
   for i in range(n//2, -1, -1):
      heapify(arr, n, i)
   for i in range(n-1, 0, -1):
      # Swap
      arr[i], arr[0] = arr[0], arr[i]
      # Heapify root element
      heapify(arr, i, 0)

arr = [1, 12, 9, 5, 6, 10]
heapSort(arr)
n = len(arr)
print("Sorted array is")
for i in range(n):
   print(arr[i], end=' ')

TIME COMPLEXITY - O(n logn)

raja
Published on 11-Jun-2021 12:34:15
Advertisements