2. Heap Sort
“Heap sort is a comparison-based sorting technique based on Binary Heap data structure.
It is similar to the selection sort where we first find the minimum element and place the
minimum element at the beginning. Repeat the same process for the remaining elements.”
Heap sort is an in-place algorithm.
Its typical implementation is not stable, but can be made stable. It is 2-3 times slower than
well-implemented Quick Sort. The reason for slowness is a lack of locality of reference.
3. Advantages of Heap Sort:
Efficiency – The time required to perform Heap sort increases logarithmically while other
algorithms may grow exponentially slower as the number of items to sort increases. This
sorting algorithm is very efficient.
Memory Usage – Memory usage is minimal because apart from what is necessary to hold
the initial list of items to be sorted, it needs no additional memory space to work.
Simplicity – It is simpler to understand than other equally efficient sorting algorithms
because it does not use advanced computer science concepts such as recursion.
4. Applications of Heap Sort:
Heap Sort is mainly used in hybrid algorithms .
Sort a nearly sorted array.
The heap sort algorithm has limited uses because Quicksort and Merge sort are better in
practice. Nevertheless, the Heap data structure itself is enormously used.
5. What is meant by 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.
6. Min Heap & Max Heap
Min Heap:
where the value of root node is less than or equal to either of its children.
Max Heap:
where the value of root node is greater than or equal to either of its children.
8. Working Of Heap Sort
Array = {4,6,10,9,2}
Corresponding Complete Binary Tree is:
Heap:
4
/
6 10
/
9 2
The task to build a Max-Heap from above array.
Swap 2 and 6:
4
/
2 10
/
9 6
9. Working Of Heap Sort
Again swap 4 & 2:
2
/
4 10
/
9 6
As, parent is smaller than children, we created MinHeap.
Now, Delete Input to reach final output: (always delete from root)
2
/
4 10
/
9 6
10. Working Of Heap Sort
Element “2” will be deleted as it is the root element and we will shift the Last Most Right element “6” to the
beginning.
6
/
4 10
/
9
Output Array:{2}
As it is not in MinHeap, first Heapify the binary tree.
Swap 4 and 6:
4
/
6 10
/
9
11. Working Of Heap Sort
Again Element “4” will be deleted as it is the root element and we will shift the Last Most Right
element “9” to the beginning.
9
/
6 10
Output Array:{2,4)
As it is not in MinHeap, first Heapify the binary tree.
Swap 6 and 9:
6
/
9 10
12. Working Of Heap Sort
Again Element “6” will be deleted as it is the root element and we will shift the Last Most Right
element “10” to the beginning.
10
/
9
Output Array:{2,4,6)
As it is not in MinHeap, first Heapify the binary tree.
Swap 9 and 10:
9
/
10
13. Working Of Heap Sort
Repeat the same process. Delete the root element “9” and the Last Most Right element “10” is
the last element so it will be placed into the final array.
Output Array:{2,4,6,9,10).