Heap Sort Algorithm: Relationship Between Array Indexes and Tree Elements
Heap Sort Algorithm: Relationship Between Array Indexes and Tree Elements
= element in 1 index
= 12
Right child of 1
= element in 2 index
= 9
Similarly,
= element in 3 index
= 5
Right child of 12
= element in (2*1+2) index
= element in 4 index
= 6
Let us also confirm that the rules hold for finding parent of any
node
Parent of 9 (position 2)
= (2-1)/2
= ½
= 0.5
~ 0 index
= 1
Parent of 12 (position 1)
= (1-1)/2
= 0 index
= 1
heapify(array)
Root = array[0]
Largest = largest( array[0] , array [2*0 + 1]. array[2*0+2])
if(Root != Largest)
Swap(Root, Largest)
Heapify base cases
The example above shows two scenarios - one in which the
root is the largest element and we don't need to do anything.
And another in which the root had a larger element as a
child and we needed to swap to maintain max-heap
property.
If you're worked with recursive algorithms before, you've
probably identified that this must be the base case.
Now let's think of another scenario in which there is more
than one level.
How to heapify root element when its subtrees are already max heaps
The top element isn't a max-heap but all the sub-trees are
max-heaps.
To maintain the max-heap property for the entire tree, we
will have to keep pushing 2 downwards until it reaches its
correct position.
How to heapify root element when its subtrees are max-heaps
This function works for both the base case and for a tree of
any size. We can thus move the root element to the correct
position to maintain the max-heap status for any tree size as
long as the sub-trees are max-heaps.
Build max-heap
To build a max-heap from any tree, we can thus 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.
In the case of a complete tree, the first index of a non-leaf
node is given by n/2 - 1. All other nodes after that are leaf-
nodes and thus don't need to be heapified.
So, we can build a maximum heap as
2. Swap: Remove the root element and put at the end of the array
(nth position) Put the last item of the tree (heap) at the vacant
place.
3. Remove: Reduce the size of the heap by 1.
4. Heapify: Heapify the root element again so that we have the
highest element at root.
5. The process is repeated until all the items of the list are sorted.
Swap, Remove, and Heapify
Heap Sort Complexity
Heap Sort has O(nlog n) time complexities for all the cases ( best
case, average case, and worst case).
Let us understand the reason why. The height of a complete binary
tree containing n elements is log n
During the sorting step, we exchange the root element with the last
element and heapify the root element. For each element, this again
takes log n worst time because we might have to bring the element
all the way from the root to the leaf. Since we repeat this n times, the
heap_sort step is also nlog n.
Also since the build_max_heap and heap_sort steps are executed one
after another, the algorithmic complexity is not multiplied and it
remains in the order of nlog n.