- Time Complexity: O(log(n)) due to heapify(logn) and CreateandBuildHeap is O(n).
- Space Complexity: O(1).
- Applications: Finding the order in statistics, dealing with priority queues in Prim's algorithm and Huffman encoding or data compression.
- Founder's Name: John William Joseph Williams
- Create and build a max heap.
- Remove the root element and put at the end of the list. Put the last item of the tree at the vacant place.
- Reduce the size of the heap by 1.
- Heapify the root element again so that we have the highest element at root.
- Repeat this process until all the elements are sorted.
Given array : [4, 10, 3, 5, 1]
1. Create a heap: [4, 10, 3, 5, 1]
2. Build a max heap: [10, 5, 3, 4, 1]
3. Remove the node: [1, 5, 3, 4, 10]
4. Build a max heap: [5, 4, 3, 1, 10]
5. Remove the node: [1, 4, 3, 5, 10]
6. Build a max heap: [4, 1, 3, 5, 10]
7. Remove the node: [3, 1, 4, 5, 10]
8. Already max heap: [3, 1, 4, 5, 10]
9. Remove the node: [1, 3, 4, 5, 10]
10. Sorted Array: [1, 3, 4, 5, 10]