Heap sort
Heap sort
Tree
Lecture No.-3.1 DISCOVER . LEARN .
EMPOWER
Index
• Heap Data Structure
• Heap Operations
• Insertion in the Heap tree
• Deletion in Heap Tree
2 2
Heap Data Structure
Heap data structure is a complete binary tree that
satisfies the heap property, where any given node is
• always greater than or equal to its child node/s and the
key of the root node is the largest among all other
nodes. This property is also called max heap property.
A[Parent(i)] >= A[i]
• always smaller than or equal to the child node/s and
the key of the root node is the smallest among all other
nodes. This property is also called min heap property.
A[Parent(i)] <= A[i]
Operations of Heap
Data Structure
• Heapify: a process of creating a heap from an array.
• Insertion: process to insert an element in existing
heap time complexity O(log N).
• Deletion: deleting the top element of the heap or
the highest priority element, and then organizing
the heap and returning the element with time
complexity O(log N).
• Peek: to check or find the most prior element in the
heap, (max or min element for max and min heap).
Types of Heap Data
Structure
Generally, Heaps can be of two types:
• Max-Heap: In a Max-Heap the key present at the root
node must be greatest among the keys present at all
of it’s children. The same property must be recursively
true for all sub-trees in that Binary Tree.
A[Parent(i)] >= A[i]
• Min-Heap: In a Min-Heap the key present at the root
node must be minimum among the keys present at all
of it’s children. The same property must be recursively
true for all sub-trees in that Binary Tree.
A[Parent(i)] <= A[i]
Max Heap
• A[Parent(i)] >= A[i]
Min Heap
A[Parent(i)] <= A[i]
Heapify
Heapify is the process of creating a heap data
structure from a binary tree represented using an
array. It is used to create Min-Heap or Max-heap.
Start from the first index of the non-leaf node whose
index is given by n/2 – 1. Heapify uses recursion.
Algorithm for Heap
sort:
MaxHeapify(arr,i)
L = left(i)
R = right(i)
if L ? heap_size[arr] and arr[L] > arr[i]
largest = L
else
largest = i
if R ? heap_size[arr] and arr[R] > arr[largest]
largest = R
if largest != i
swap arr[i] with arr[largest]
MaxHeapify(arr,largest)
End
Continue..
BuildMaxHeap(arr)
heap_size(arr) = length(arr)
for i = length(arr)/2 to 1
MaxHeapify(arr,i)
End
Continue..
HeapSort(arr)
BuildMaxHeap(arr)
for i = length(arr) to 2
swap arr[1] with arr[i]
heap_size[arr] = heap_size[arr] ? 1
MaxHeapify(arr,1)
End
Working of Heap sort
Algorithm
In heap sort, basically, there are two phases involved
in the sorting of elements.
• The first step includes the creation of a heap by
adjusting the elements of the array.
• After the creation of heap, now remove the root
element of the heap repeatedly by shifting it to the
end of the array, and then store the heap structure
with the remaining elements.
Example Of Heap Sort
let's take an unsorted array
In the next step, again we have to delete the root element (54) from the
max heap. To delete this node, we have to swap it with the last node,
i.e. (14). After deleting the root element, we again have to heapify it to
convert it into max heap.
Continue..
After swapping the array element 54 with 14 and converting
the heap into max-heap, the elements of array are -
24