Heap Sort

Heapsort requires the following operation to get the sorted list. Lets first understand the following operations:

Heapify: Rearrange the remaining elements to maintain the heap property.

Build Max Heap: Construct a max heap from the input array. This involves arranging the elements in the array to satisfy the heap property.

Extract Maximum: Remove the root element (which is the maximum value) from the heap and place it at the end of the array.

Heapify

Heapify operation is applied on a node and ensures that this nodes fulfills the structure and order propoerty of heap. Algorithm is as follows

STEPS
index-> 0123456
4(i)2(L)6(R)1357
64
7(i)(L)4(R)
6271357

Build-Max-Heap

This procedure converts any sequence to a Heap, Basically it applies Max-Heapify operation to all non-leaf nodes as follows

Example- Convert the following tree into Max-Heap

Heap-Extraxt-Max

This operation extracts the maximum element from the Max-Heap, which is the root node of the heap. The logic is to remove the first element and fill this space by last element. The resultant is no more a heap, so Max-heapify (A,0) operation is applied to convert it into heap. The resultant is heap again with size reduces by 1.

Example- Extract Max value for the following heap

index->0123456
7361254

Published by Expert_Talk

I like sharing article and discussion on emerging technologies. Love to share my knowledge on various latest trends and technologies.

One thought on “Heap Sort

Leave a comment