Professional Documents
Culture Documents
Complied by :
Ashwin Prakash Srivastava
S.R. No. 234/05
3rd I.T.
HEAP SORT
It is one of the efficient
techniques of sorting .
It involves two phases
1. Constructing the heap.
2. Repeatedly deleting the root
element of the heap and
reforming the heap of the
remaining elements
Heap sorting is basically an
improvement over Binary Tree Sort. In
Binary Tree Sort we are required to
create the nodes to form the binary
tree and the Inorder traversal of the
resulting Binary Tree gives the sorted
list of elements. But in heap sort we
simply build a heap by adjusting the
position of the elements in the array
itself.
Heap sort introduces the use of a
data structure called as HEAP to
manage information during the
execution of the algorithm.
So first lets have an insight into
what is called as HEAP.
HEAP
HEAP is a Complete Binary Tree
A tree is said to be a Complete
Binary Tree if all its levels except
possibly the last have the
maximum number of possible
nodes and if all the nodes at the
last level appear as far left as
possible.
TYPES OF HEAP
Heaps are of two types
1. MAX HEAP
2. MIN HEAP
MAX HEAP
In Max Heap the value at any
node is greater than or equal to
the value at each of the children.
It is also called as Descending
Heap
MAX HEAP
36
32 29
17 22 20 28
16 14 15
MIN HEAP
In Min Heap the value at any node
is smaller than or equal to the
value at each of the children. It is
also called as Ascending Heap
MIN HEAP
5 12
10 15 87 28
57 65 75
BUILDING A HEAP
First step to create a heap is to
imagine a binary tree that can be
built from the given array
elements.
A binary tree is created using the
formula that if arr[i] denotes the
root element then its left child
would be arr[2i+1] and right child
would be arr[2i+2].
CREATING A BINARY
TREE
Given array be
ARRAY ELEMEMTS
11 2 9 13 57 25 17 1 90 3
0 1 2 3 4 5 6 7 8 9
ARRAY INDEX
BINARY TREE
11
9
2
13 57 25 17
1 90 3
11 2 9 13 57 25 17 1 90 3
MAKING A HEAP (MAX)
11
9
2
13 57 25 17
1 90 3
11 2 9 13 57 25 17 1 90 3
MAKING A HEAP
11
9
2
13 57 25 17
1 90 3
11 2 9 13 57 25 17 1 90 3
MAKING A HEAP
11
9
2
90 57 25 17
1 13 3
11 2 9 13 57 25 17 1 90 3
MAKING A HEAP
11
9
2
90 57 25 17
1 13 3
11 2 9 13 57 25 17 1 90 3
MAKING A HEAP
11
25
2
90 57 9 17
1 13 3
11 2 9 13 57 25 17 1 90 3
MAKING A HEAP
11
9
2
90 57 25 17
1 13 3
11 2 9 13 57 25 17 1 90 3
MAKING A HEAP
11
9
90
2 57 25 17
1 13 3
11 2 9 13 57 25 17 1 90 3
MAKING A HEAP
11
9
90
13 57 25 17
1 2 3
11 2 9 13 57 25 17 1 90 3
MAKING A HEAP
11
9
90
13 57 25 17
1 2 3
11 2 9 13 57 25 17 1 90 3
MAKING A HEAP
90
9
11
13 57 25 17
1 2 3
11 2 9 13 57 25 17 1 90 3
MAKING A HEAP
90
9
57
13 11 25 17
1 2 3
11 2 9 13 57 25 17 1 90 3
HEAP
90
25
57
13 11 9 17
1 2 3
SECOND PHASE OF
HEAP SORT
The phase involves deletion of the
root element and restoration of
the heap structure. The deleted
elements are collected in the
order they are deleted and at the
end of the process we obtain the
sorted list of the elements
DELETION OF
ELEMENTS
90
25
57
13 11 9 17
1 2 3
90 57 25 13 11 9 17 1 2 3
DELETION OF
ELEMENTS
57
25
13
3 11 9 17
1 2
57 13 25 3 11 9 17 1 2 90
ADJUSTMENTS
90
25
57
13 11 9 17
1 2 3
90 57 25 13 11 9 17 1 2 3
ADJUSTMENTS
25
57
13 11 9 17
1 2
90 57 25 13 11 9 17 1 2 3
ADJUSTMENTS
57
25
3
13 11 9 17
1 2
90 57 25 13 11 9 17 1 2 3
ADJUSTMENTS
57
25
13
3 11 9 17
1 2
90 57 25 13 11 9 17 1 2 3
DELETION OF
ELEMENTS
57
25
13
3 11 9 17
1 2
57 13 25 3 11 9 17 1 2 90
DELETION OF
ELEMENTS
25
17
13
3 11 9 2
25 13 17 3 11 9 2 1 57 90
DELETION OF
ELEMENTS
17
9
13
3 11 1 2
17 13 9 3 11 1 2 25 57 90
DELETION OF
ELEMENTS
13
9
11
3 2 1
13 11 9 3 2 1 17 25 57 90
DELETION OF
ELEMENTS
11
9
3
1 2
11 3 9 1 2 13 17 25 57 90
DELETION OF
ELEMENTS
9
2
3
9 3 2 1 11 13 17 25 57 90
DELETION OF
ELEMENTS
3
2
1
3 1 2 9 11 13 17 25 57 90
DELETION OF
ELEMENTS
2
2 1 3 9 11 13 17 25 57 90
DELETION OF
ELEMENTS
1
1 2 3 9 11 13 17 25 57 90
SORTED ELEMENTS
LIST
1 2 3 9 11 13 17 25 57 90
COMPLEXITY OF HEAP
SORT
We observe that the number of
comparisons to find the appropriate
place of a new element cannot exceed
the depth of the heap. Since heap is a
Complete Binary Tree so its depth is
bounded by log n where n is the
number of elements in the heap.
Accordingly the total number g(n) of
comparisons to insert the n elements
is bounded as follows
g(n) <= n log n
DEPTH OF A TREE
Consider a binary tree. Each node can have at most
two children. The total number of nodes is n
If the level of the tree is r then the total number of
nodes will be 2r (at most)
2r + 2r-1 + 2r-2 ….. 20 >= n
L.H.S. is a G.P. whose sum is (2r+1 - 1)
(2r+1 - 1) >= n 2r+1 >= n
taking log both sides , we get
log 2r+1 >= log n r+1 >= log n
Depth = level+1 d=r+1
d >= log n
So we can say that depth is bounded by log n
MORE TIGHTLY BOUND
We can derive a much tighter bound
by observing that the time O(lg n)
varies with the height of the node in
the tree and the height of most nodes
are small.
The tighter analysis relies on the
property that in an n-element heap or
full binary tree there are n/2(rounded
off to nearest greatest integer) nodes
at height 0 i.e. leaf nodes, n/4 nodes at
height 1 and similarly (n/2h+1) nodes at
height h.
So the cost of building a heap is :
lg n lg n
Σ [ n / 2h+1 ] O(h) O [ n Σ (h / 2h) ] ---(1)
h=0 h=0
Consider sum of a G.P. for infinite
number of terms such that common
ratio |x|<1
∞
Σ [ xk ] = 1/(1-x) Σ [ kxk-1 ] = 1/(1-x)2
k=0
we get x=1/2
∞
Σ [ h / 2h ] = (1/2) / (1- (1/2))2 = 2
h=0
Thus running time can be
bounded as :
lg n ∞
O [ n Σ (h / 2h) ] = O [ n Σ (h / 2 ) ] ---(1) =
h
O(n)
h=0 h=0
29
32
17 22 20 28
16 14 15
LET 24 is to be added
INSERTION RULE FOR
THE HEAP
If the index of the root is
considered as 1 then the left and
right child of ith node are
present at (2i)th and (2i+1)th
position respectively and the
parent node is present at(i/2)th
index in the array
array, so the index of the added
element will be 11, so its parent
will be (i/2) .
(11/2) = 5 rounded off and
arr[5]=22. 36
29
32
17 22 20 28
16 14 15 24
LET 24 is to be added
Now the adjustments will be
done
36
29
32
17 24 20 28
16 14 15 22
LET 24 is to be added
The heap after adding new
element
36
29
32
17 24 20 28
16 14 15 22
Thank you