Upcoming SlideShare
×

# Heapsort heapify

• 39 views

• Comment goes here.
Are you sure you want to
Be the first to comment
Be the first to like this

Total Views
39
On Slideshare
0
From Embeds
0
Number of Embeds
2

Shares
0
0
Likes
0

No embeds

### Report content

No notes for slide

### Transcript

• 1. THE HEAPSORT ALGORITHM WITH HEAPIFY. Consider a set of elements {12,34,56,73,24,11,34,56,78,91,34,91,45}. Sketch the heapsort algorithm and use it to sort this set. Obtain a derivation for the time complexity of heapsort, both the worst case and average case behaviour. COMPLEXITY TO BUILD HEAP WITH HEAPIFY O(n) COMPLEXITY ANALYSIS OF HEAPIFY
• 2. SIMULATION OF HEAPIFY INITIAL 12 34 1 2 56 3 73 4 24 5 11 6 34 7 56 8 78 9 91 10 34 11 91 12 45 13 12,1 56,3 34,2 73,4 56,8 11,6 24,5 91,10 78,9 34,11 91,12 34,7 45,13 Number of elements = 13 = n; I=floor(n/2)=6 So consider the heap with 6 as root, the left subtree is a one element heap, the right subtree is a one element heap, and the root may be violating the heap property. So 11 comes down and 91 becomes the 6th node. 12 1 34 2 56 3 73 4 24 5 91 6 34 7 56 8 78 9 91 10 34 11 12,1 56,3 34,2 73,4 56,8 78,9 91,6* 24,5 91,10 34,11 11,12 34,7 45,13 11 12 45 13
• 3. Now let I=5. We have the heap with 5 as root and left subtree is a one element heap and the right subtree is a one element heap. The element at 5 is violating the heap property, so let 91 come up to position 5, and 24 go down to position 10. 12 1 34 2 56 3 73 4 91 5 91 6 34 7 56 8 78 9 24 10 34 11 11 12 45 13 12,1 56,3 34,2 73,4 56,8 91,6* 91,5* 24,10 78,9 34,11 11,12 34,7 45,13 Now let I=4. We have the heap with 4 as root and left subtree is a one element heap and the right subtree is a one element heap. The element at 4 is violating the heap property, so let 78 come up to position 4, and 73 go down to position 9. 12 1 34 2 56 3 78 4 91 5 91 6 34 7 56 8 73 9 24 10 34 11 12,1 56,3 34,2 78,4* 56,8 73,9 91,6* 91,5* 24,10 34,11 11,12 34,7 45,13 11 12 45 13
• 4. Now let I=3. We have the heap with 3 as root and left subtree is a heap and the right subtree is a one element heap. The element at 3 is violating the heap property, so let 91 come up to position 3, and 56 go down to position 6. 12 1 34 2 91 3 78 4 91 5 56 6 34 7 56 8 73 9 24 10 34 11 11 12 45 13 12,1 91,3* 34,2 78,4* 56,8 56,6* 91,5* 24,10 73,9 34,11 11,12 34,7 45,13 Now let I=2. We have the heap with 2 as root and left subtree is a heap and the right subtree is a heap. The element at 2 is violating the heap property, so let 91 come up to position 2, and 34 go down to position 5. 12 1 91 2 91 3 78 4 34 5 56 6 34 7 56 8 73 9 24 10 34 11 12,1 91,3* 91,2* 78,4* 56,8 73,9 56,6* 34,5* 24,10 34,11 11,12 34,7 45,13 11 12 45 13
• 5. Now let I=1. We have the heap with 1 as root and left subtree is a heap and the right subtree is a heap. The element at 1 is violating the heap property, so let 91 come up to position 1, and 12 go down to position 2. 12 1 91 2 91 3 78 4 34 5 56 6 34 7 56 8 73 9 24 10 34 11 11 12 45 13 12,1 91,3* 91,2* 78,4* 56,8 56,6* 34,5* 24,10 73,9 34,11 11,12 34,7 45,13 Now let I=1. We have the heap with 1 as root and left subtree is a heap and the right subtree is a heap. The element at 1 is violating the heap property, so let 91 come up to position 1, and 12 go down to position 2. 91 1 12 2 91 3 78 4 34 5 56 6 34 7 56 8 73 9 24 10 34 11 91,1* 91,3* 12,2* 78,4* 56,8 73,9 56,6* 34,5* 24,10 34,11 11,12 34,7 45,13 11 12 45 13
• 6. Now 12 is compared with its two children, 78 came up and 12 comes down, 91,1* 91,3* 78,2* 12,4* 56,8 56,6* 34,5* 24,10 73,9 34,11 11,12 34,7 45,13 Now 12 is compared with its two children, 73 moves up and 12 moves down. 91,1* 91,3* 78,2* 73,4* 56,8 12,9 56,6* 34,5* 24,10 34,11 11,12 34,7 45,13 THE HEAP HAS BEEN FORMED AT LAST. NOW HEAPSORT CAN BE IMLEMENTED, THE SIMULATION IS AS IN http://www.gateguru.com/algorithms/heapsort.pdf