When talking about Computer Science, sorting algorithms are one of the most essential part of the theme. These algorithms can be different from each other owing to its efficiency and the time that is taken to finish the task. Despite the fact that there are a myriad of sorting algorithms, today we’ ll learn the algorithm with the time complexity of O(n lg(n)) which is averagely so fast.
A heap sort is a sorting algorithm based on the binary heap data structure. The idea behind a heap sort is to find the highest value and place it at the end, repeating the process until everything is sorted.
In order to fully understand how a heap sort works, we first have to understand a binary heap, and subsequently, a binary tree. A binary tree is a tree data structure where each element has at most 2 children. A binary heap is simply a complete binary tree, in which each level of the tree (except, perhaps, the last level) is completely filled and all nodes are stored starting from the left to right. A binary heap also takes into account how the values are stored. Either the value of the parent node is always greater than the value of its children (called a max heap), or vice versa, where the value of the parent is smaller than that of its children (called a min heap). A heap sort will utilize a max heap to sort its elements.
Heap is a tree-based data structure in which all the nodes of the tree are in a specific order. Heap is always a complete binary tree. While converting a Heap to an array we must keep in mind the following:
The root element of the heap should be at index 0 in the array.
For the node at index i:
At index (i-1)/2 is the parent node
At index (2*i)+1 is the left child node
At index (2*i)+2 is the right child node
Heaps are of the following two types:
In this type of heap, the value of the parent node is always larger than the child nodes. Hence, the root node is occupied by the largest value in the whole heap. To understand how to convert a heap into a max heap, let’s see the example below.
Complexity for max heap in O(logN).
In this type of heap, the value of the parent will always be less than both of the children. Hence, the least value in the whole heap is at the root of the heap. To understand this better let's see this quick example.
HEAP SORT ALGORITHM
There are various kinds of sorting methods used in sorting different kinds of data structures. One of the popular and efficient sorting methods in computer science is Heap Sort which requires the knowledge of arrays and trees/heap. Now let us see how heap sort works and what steps we have to use.
1. From the elements of the given array, we have to make a max heap. We can start heapifying each sub-tree from the bottom up and end up with a max-heap after the function is applied to all the elements including the root element.
2. Remove the root element and put it at the end of the array (nth position) Put the last item of the tree (heap) at the vacant place.
3. Reduce the size of the heap by 1.
4. Heapify the root element again so that we have the highest element at root.
5. Keep repeating this process again and again until all the items of the list are sorted in the array successfully.
HEAP SORT APPLICATION
Systems concerned with security and embedded systems such as Linux Kernel use Heap Sort because of the O(nlog n) upper bound on Heapsort's running time and constant o(1) upper bound on its auxiliary storage.
Heap can be efficiently used if we want to extract the smallest (or largest) from the list of items without the overhead of keeping the remaining items in the sorted order. For e.g Priority Queues.
Implementation in Python
Implementation in C++
Time complexity of heapify is O(Logn). Time complexity of createAndBuildHeap() is O(n) and the overall time complexity of Heap Sort is O(nLogn).
With this article at Logicmojo, you must have the complete idea of analyzing Heap Sort algorithm.
Good Luck & Happy Learning!!