1. HEAPSORT
Name – Dinesh Kumar and Himanshu Sharma
Roll No. – 16032 and 16026 respectively
Submitted To : - Miss. Purnima Bindal
2. Heap: A special form of complete binary tree that key value of each node
is no smaller (larger) than the key value of its children (if any).
Max-Heap: Root node has the largest key.
A max tree is a tree in which the key value in
each node is no smaller than the key values in
its children. A max heap is a complete binary
tree that is also a max tree.
Min-Heap: root node has the smallest key.
A min tree is a tree in which the key value in
each node is no larger than the key values in
its children. A min heap is a complete binary
tree that is also a min tree.
Ex.
Lowest element
on top
Highest
element on top
3. 61
61
Current Node
Visited node
swapping
Comparison between nodes
Abbrevations for diagrams
Current Node
Visited node
swapping
Comparison between nodes
It is a well-known, traditional sorting algorithm
you will be expected to know.
Heapsort is always O(n log n)
* Heapsort is better in time-critical
applications.
A Binary Tree has the Heap Property iff,
* It is empty
* The key in the root is large than that in either
and both subtrees have the Heap property.
5. Function main
Input size of array n
Input array a[n]
Heapsort(a,n)
Function Heapsort (a [ ] , n)
t=n-1
for i = t to i >= 0
Build_Maxheap(a,i)
i = i - 1
swap(a,0,i)
Function swap (a [ ] , p , n)
temp=a[n]
a[n]=a[p]
a[p]=temp
Function max_heapify(a [ ] , i , n)
l=2*i+1
r=2*i+2
If l<=n && a[ l ]>a[ i ]
largest=l
else
largest=i
If r<=n && a[ r ] > a[ largest ]
largest=r
If largest ! = i
swap (a [ ] , i , largest)
max_heapify (a ,largest ,n )
Function Build_Maxheap(a [ ] , n)
For i=n/2 to i>=0
max_heapify(a ,i , n)
i = i - 1
11. 61[1]
48[2] 59[3]
15[4] 19[5] 11[6]
26[7]
5[8] 1[9] 77[10]
59[1]
48[2] 26[3]
15[4] 19[5] 11[6]
1[7]
5[8] 61[9] 77[10]
(a)
(b)
Element Removed
from the tree
swap first element with last
Decrease the tree size with 1
Max_heapify the whole tree
Step 1 :>
Step 2 :>
Step 3 :>
Step 1 :> swap (a [0 ] , a[10])
Step 2 :> Decrease the tree size with 1
Step 3 :> Max_heapify the whole tree
12. 48[1]
19[2] 26[3]
15[4] 5[5] 11[6]
1[7]
59[8] 61[9] 77[10] 26[1]
19[2] 11[3]
15[4] 5[5] 1[6]
48[7]
59[8] 61[9] 77[10]
(c)
(d)
59
6159
48
Step 1 :> swap (a [0 ] , a[last])
Step 2 :> Decrease the tree size with 1
Step 3 :> Max_heapify the whole tree
swap first element with lastStep 1 :>
Step 2 :>
Step 3 :>
Decrease the tree size with 1
Max_heapify the whole tree
13. 19[1]
15[2] 11[3]
1[4] 5[5] 26[6]
1[7]
59[8] 61[9] 77[10] 15[1]
5[2] 11[3]
1[4] 5[5] 1[6]
48[7]
59[8] 61[9] 77[10]
(e)
(f)
59
6159
48
4826
2619
Step 2 :> Decrease the tree size with 1
Step 3 :> Max_heapify the whole tree
swap first element with lastStep 1 :>
Step 2 :>
Step 3 :>
Decrease the tree size with 1
Max_heapify the whole tree
Step 1 :> swap (a [0 ] , a[last])
14. 11[1]
5[2] 1[3]
1[4] 5[5] 26[6]
1[7]
59[8] 61[9] 77[10] 5[1]
1[2] 1[3]
1[4] 5[5] 1[6]
48[7]
59[8] 61[9] 77[10]
(g)
(h)
59
6159
48
4826
2619
1915
15
11
Step 2 :> Decrease the tree size with 1
Step 3 :> Max_heapify the whole tree
swap first element with lastStep 1 :>
Step 2 :>
Step 3 :>
Decrease the tree size with 1
Max_heapify the whole tree
Step 1 :> swap (a [0 ] , a[last])
15. • So finally our result is results
1[1]
1[2] 1[3]
1[4] 5[5] 1[6]
48[7]
59[8] 61[9] 77[10]
(i)
59
48261915
115
77 61 59 48 26 19 15 11 5 1
Step 2 :> Decrease the tree size with 1
Step 3 :> Max_heapify the whole tree
Step 1 :> swap (a [0 ] , a[last])
16. Analysis I
Here’s how the algorithm starts:
heapify the array;
Heapifying the array: we add each of n nodes
Each node has to be sifted up, possibly as far as the root
Since the binary tree is perfectly balanced, sifting up a single node
takes O(log n) time
Since we do this n times, heapifying takes n*O(log n) time,
that is, O(n log n) time
17. Analysis II
Here’s the rest of the algorithm:
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}
We do the while loop n times (actually, n-1 times), because
we remove one of the n nodes each time
Removing and replacing the root takes O(1) time
Therefore, the total time is n times however long it takes the
reheap method
18. Analysis III
To reheap the root node, we have to follow one path from the root
to a leaf node (and we might stop before we reach a leaf)
The binary tree is perfectly balanced
Therefore, this path is O(log n) long
And we only do O(1) operations at each node
Therefore, reheaping takes O(log n) times
Since we reheap inside a while loop that we do n times, the total
time for the while loop is n*O(log n), or O(n log n)
19. Analysis IV
Here’s the algorithm again:
heapify the array;
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}
We have seen that heapifying takes O(n log n) time
The while loop takes O(n log n) time
The total time is therefore O(n log n) + O(n log n)
This is the same as O(n log n) time
20. Thank you
Name – Dinesh Kumar and Himanshu Sharma
Roll No. – 16032 and 16026 respectively
Submitted To : - Miss. Purnima Bindal