Introduction:
The heapsort algorithm is based on the comparison sorting technique. It can be thought of as an improved version of the selection sort.
Just like the selection sort, heapsort divides the whole input array into a sorted and unsorted part and with continuous iterative sessions, it keeps on recoiling the size of the unsorted array by adding the elements at appropriate positions. Just think of the time of morning assembly in primary schools, children rushed to the prayer ground and just stood in their class lines anyhow. They are not concerned about the arrangement. The teacher used to arrange the line according to their height. Heapsort is just similar to that, it also arranges the array elements one after another in proper sorted order, and the unsorted array part keeps on decreasing.

(Source: Giphy)
Now, you might be thinking that you already know some of the sorting techniques, then what's the use of knowing another??
Different algorithms have different time and space complexities, so serving heap sort in the plate is as essential as any other algorithms.
So, if you don’t know about heap sort already, just hang in there with me because together, we will sneak peek all about heap sort in this article.

Source: Giphy
Working of Heap Sort:
As we already know, that Heap sort is used to sort the array either in ascending or descending order, but then how does it implement a comparison-based sorting algorithm??
In heap sort, our primary task is to build the max heap and then continue swapping the root element with the last element. After every step, we need to ensure that the heap property is maintained.
Let's consider an example :
Input array:-
| 10 | 15 | 21 | 30 | 18 | 19 |
These are the keys for performing heap sort. The output array should look like this:
| 10 | 15 | 18 | 19 | 21 | 30 |
In our example, as we are converting the whole input array into ascending order, we will build the first max heap.
Step 1: Now our first step will be, will make a binary tree, with these array elements, so our first child will be: (2* Index + 1) and our right child will be (2 *Index + 2).
Step 2: Now our next step is, we will convert this binary tree to the max heap. Now, we will take the last non-leaf node and will apply there heapify down. Here, 21 will be the first non-leaf node and will apply there heapify down. But 19<21. So, no change, they are already in the correct place. We will not swap.
Step 3: coming to the next leaf node, 15 have two children: 30 and 18, but unfortunately,
30> 15, hence the property of a heap is violated here. So, we will swap the two elements.
After swapping, the situation looks like this:
max-heap
From the max heap formed, the array representation will be: [30,18, 21,15,10,19]
Step 4: now, we will start removing one after the other node and will swap with the last node.
So, 30 will be removed from the heap and 19 will get placed in its position, and then again, down heapify will be applied over there. And the array representation will look like this:
| 19 | 18 | 21 | 15 | 10 | 30 |
heapify
Step 5: Now again, 21 will be compared with the last element, removed from the heap, and will get placed in the correct position in the array list.
| 10 | 18 | 19 | 15 | 21 | 30 |
heapify
Step 6: The same process will get repeated until the whole array gets sorted.
| 15 | 18 | 10 | 19 | 21 | 30 |
heapify
| 10 | 15 | 18 | 19 | 21 | 30 |
heapify
| 10 | 15 | 18 | 19 | 21 | 30 |
heapify
Array representation : [10, 15, 18, 19, 21, 30] which is a sorted array.




