Professional Documents
Culture Documents
Array Index 0 1 2 3 4 5 6
Input 40 60 10 20 50 30
Here 6 nodes
N=6
Floor(N/2)
= floor(6/2)
= floor (3)
=3
• Min Heap the parent
=> We start comparison from 3
node is always smaller
than or equal to its child
nodes.
• Is there any child node smaller than 10?
No
• So we move to index 2 or the 2nd node.
0 1 2 3 4 5 6
Array Index
10
Sorted Element
• Put the last node in the root position.
• Now there are 5 nodes. N = 5
• = floor (N/2)
• = floor (5/2)
• = 2.5
• = 2 (Start from 2 node)
• Any child node smaller than 20?
• No.
• So we move to index 1 or the 1st node
0 1 2 3 4 5 6
Array Index
10 20
Sorted Element
• Put the last node in the root position.
• Now there are 4 nodes. N = 4
• = floor (N/2)
• = floor (4/2)
• = 2 (Start from 2 node)
Array Index 0 1 2 3 4 5 6
10 20 30
Sorted Element
• Delete the root node
• Put last node in the root node
• 3 nodes N= 3
• = Floor (3/2)
• = Floor (1.5)
• = 1 (So we start from 1)
• Child node smaller than 60?
• Yes its 40
• Swap 40 & 60
Array Index 0 1 2 3 4 5 6
10 20 30 40
Sorted Element
5 4 3 1 2 6
0 1 2 3 4 5
1 2 3 4 5 6
• Merge Sort follows recursive algorithms
• We divide the array into halves till the sub array has only 1
element
• Major work is done in merging the sub arrays
• We need extra temporary array of the same size as the input
array of merging.
Tree of calls of Merge Sort(l,10)
Thank you