0% found this document useful (0 votes)
8 views29 pages

Heap

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views29 pages

Heap

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

G.L.

BAJAJ INSTITUTE OF TECHNOLOGY & MANAGEMENT


GREATER NOIDA

Heap Sort Algorithm


CSE Department

apsort - 1
Heapsort
Heapsort
• What is a heap? Max-heap? Min-heap?
• Maintenance of Max-heaps
- MaxHeapify
- BuildMaxHeap
• Heapsort
- Heapsort
- Analysis
Heapsort
• Combines the better attributes of merge sort
and insertion sort.
» Like merge sort, but unlike insertion sort, running
time is O(nlgn).
» Like insertion sort, but unlike merge sort, sorts in
place.
• Introduces an algorithm design technique
» Create data structure (heap) to manage information
during the execution of an algorithm.
• The heap has other applications beside sorting.

apsort - 3
Data Structure Binary Heap
• Array viewed as a nearly complete binary tree.
• Physically – linear array.
• Logically – binary tree, filled on all levels (except lowest.)
• Map from array elements to tree nodes and vice versa
• Root – A[1], Left[Root] – A[2], Right[Root] – A[3]
• Left[i] – A[2i]
• Right[i] – A[2i+1] A[2] A[3]
• Parent[i] – A[i/2]
A[i]

A[2i] A[2i + 1]

apsort - 4
Data Structure Binary Heap
• length[A] – number of elements in array A.
• heap-size[A] – number of elements in heap stored in A.
» heap-size[A]  length[A]

24 21 23 22 36 29 30 34 28 27 1 24
1 2 3 4 5 6 7 8 9 10
2 21 3 23
Searching the tree in breadth-first
fashion, we will get the array.
4 22 5 36 6 29 7 30

8 34 9 28 27 10

apsort - 5
Heap Property (Max and Min)
 Max-Heap
» For every node excluding the root, the value stored in that
node is at most that of its parent: A[parent[i]]  A[i]
 Largest element is stored at the root.
 In any subtree, no values are larger than the value
stored at subtree’s root.
 Min-Heap
» For every node excluding the root, the value stored in that
node is at least that of its parent: A[parent[i]]  A[i]
 Smallest element is stored at the root.
 In any subtree, no values are smaller than the value
stored at subtree’s root

apsort - 6
Heaps – Example
26 24 20 18 17 19 13 12 14 11 Max-heap as an
1 2 3 4 5 6 7 8 9 10
array.

Max-heap as a binary
tree. 1 26

2 24 3 20

4 18 5 17 6 19 7 13

8 12 9 14 11 10 Last row filled from left to right.


apsort - 7
Heaps in Sorting
• Use max-heaps for sorting.
• The array representation of a max-heap is not sorted.
• Steps in sorting
i) Convert a given array of size n to a max-heap
(BuildMaxHeap)
ii) Swap the first and last elements of the array.
• Now, the largest element is in the last position – where it belongs.
• That leaves n – 1 elements to be placed in their appropriate locations.
• However, the array of first n – 1 elements is no longer a max-heap.
• Float the element at the root down one of its subtrees so that the array
remains a max-heap (MaxHeapify)
• Repeat step (ii) until the array is sorted.

apsort - 8
Maintaining the heap property
 Suppose two subtrees are max-heaps,
but the root violates the max-heap
property.

 Fix the offending node by exchanging the value at the


node with the larger of the values at its children.
» May lead to the subtree at the child not being a max heap.
 Recursively fix the children until all of them satisfy the
max-heap property.

apsort - 9
MaxHeapify – Example
MaxHeapify(A, 2)

1
26 26
2 3
24
18
14 20 24
14 20

4 5 6 7
18
24
14 17 19 13 18
14
24 17 19 13

9 10
8
12 14
18 11 12 14
18 11

apsort - 10
Procedure MaxHeapify
MaxHeapify(A,
MaxHeapify(A,i)i)
1. ll
1. left(i)
left(i) (*
(*A[l]
A[l]isisthe
theleft
leftchild
childof
of Assumption:
A[i]
A[i].*).*) Left(i) and Right(i)
2. rr
2. right(i)
right(i) are max-heaps.
3. ififllheap-size[A]
3. heap-size[A]and
andA[l]
A[l]>>A[i]
A[i]
4.
4. then largest
thenlargest ll
5.
5. else largest
elselargest ii
6. ififrrheap-size[A]
6. heap-size[A]and
andA[r]
A[r]>>A[largest]
A[largest]
A[largest] must be
7.
7. then largest
thenlargest rr the largest among
8. largest ii
8. ififlargest A[i], A[l] and A[r].
9.
9. then thenexchange A[i]
exchangeA[i] A[largest]
A[largest]
10.
10. MaxHeapify(A,
MaxHeapify(A,largest)
largest)
apsort - 11
Running Time for MaxHeapify
MaxHeapify(A,
MaxHeapify(A,i)i)
1. ll
1. left(i)
left(i)
2. rr
2. right(i)
right(i)
3. ififllheap-size[A]
3. heap-size[A]and
andA[l]
A[l]>>A[i]
A[i] Time to fix node i and
4.
4. then largest
thenlargest ll its children = (1)
5.
5. else largest
elselargest ii
6. ififrrheap-size[A]
6. heap-size[A]and
andA[r]
A[r]>>A[largest]
A[largest]
PLUS
7.
7. then largest
thenlargest rr
8. largest ii
8. ififlargest Time to fix the
9.
9. then thenexchange A[i]
exchangeA[i] A[largest]
A[largest] subtree rooted at one
of i’s children =
10.
10. MaxHeapify(A,
MaxHeapify(A,largest)
largest) T(size of subree at
largest)
apsort - 12
Building a heap
 Use MaxHeapify to convert an array A into a max-heap.
 How?
 Call MaxHeapify on each element in a bottom-up
manner.

BuildMaxHeap(A)
BuildMaxHeap(A)
1. heap-size[A] 
1. heap-size[A]  length[A]
length[A]
2. for ii 
2. for  length[A]/2
length[A]/2 downto
downto 11 (*A[length[A]/2
(*A[length[A]/2+1],
+1],
3.
3. do
do MaxHeapify(A,
MaxHeapify(A, i)i) A[length[A]/2
A[length[A]/2+2],
+2],

…are
areleaf
leafnodes.*)
nodes.*)

apsort - 13
BuildMaxHeap – Example
Input Array:

24 21 23 22 36 29 30 34 28 27

Initial Heap:
1 24
(not max-heap)

2 21 3 23

4 22 5 36 6 29 7 30

8 34 9 28 27 10
apsort - 14
BuildMaxHeap – Example
MaxHeapify(10/2 = 5)
MaxHeapify(4)
MaxHeapify(3) 1 24
36
MaxHeapify(2)
MaxHeapify(1)
21
2 34
24
36 3 30
23

4 28
34
24
22 36
27
5 21 6 29 7 30
23

34
8 22 9 24
28 27
21 10

apsort - 15
Running Time of BuildMaxHeap
 Loose upper bound:
» Cost of a MaxHeapify call  No. of calls to MaxHeapify
» O(lgn)  O(n) = O(nlgn)
 Tighter bound:
» Cost of a call to MaxHeapify at a node depends on the height,
h, of the node – O(h).
» Height of most nodes smaller than lgn.
» Height of nodes h ranges from 0 to lgn.
» No. of nodes of height h is at most n/2h+1?

apsort - 16
Height
 Height of a node in a tree: the number of edges on
the longest simple downward path from the node
to a leaf.
 Height of a tree: the height of the root.
 Height of a heap: lg n 
» Basic operations on a heap run in O(lg n) time

apsort - 17
Heap Characteristics
 Height = lg n
 No. of leaves  n/2
 No. of nodes of height h  n/2h+1?

..
height(a leaf) = 0 .
 n/22+1
 n/21+1
 n/20+1

apsort - 18
Heapsort
 Sort by maintaining the as yet unsorted elements as a
max-heap.
 Start by building a max-heap on all elements in A.
» Maximum element is in the root, A[1].
 Move the maximum element to its correct final
position.
» Exchange A[1] with A[n].
 Discard A[n] – it is now sorted.
» Decrement heap-size[A] by one.
 Restore the max-heap property on A[1..n–1].
» Call MaxHeapify(A, 1).
 Repeat until heap-size[A] is reduced to 2.

apsort - 19
Heapsort(A)
HeapSort(A)
HeapSort(A)
1.
1. Build-Max-Heap(A)
Build-Max-Heap(A)
2. forii
2. for length[A]
length[A]downto
downto22
3.
3. do
doexchange A[1]
exchangeA[1] A[i]
A[i]
4.
4. heap-size[A]
heap-size[A] heap-size[A]
heap-size[A]––11
5.
5. MaxHeapify(A,
MaxHeapify(A,1)1)

apsort - 20
Heapsort – Example
26 17 20 18 24 19 13 12 14 11
1 2 3 4 5 6 7 8 9 10

26 Build-Max-heap 26

17 20 24 20

18 24 19 13 18 17 19 13

12 14 11 12 14 11

apsort - 21
26
26 11
Maxheapify
24 20 24 20

18 17 19 13 18 17 19 13

12 14 11 12 14 11
24, 26
24 11
Maxheapify
18 20 18 20

14 17 19 13 14 17 19 13

12 11 11 12 14 11

apsort - 22
20, 24, 26
20 12
Maxheapify
18 19 18 19

14 17 11 13 14 17 11 13

12 14 11 12 14 11
19, 20, 24, 26
19 12
Maxheapify
18 13 18 13

14 17 11 12 14 17 11 12

12 11 11 12 14 11

apsort - 23
18, 19, 20, 24, 26
18 11
Maxheapify
17 13 17 13

14 12 11 13 14 12 11 13

12 14 11 12 14 11
17, 18, 19, 20, 24, 26
17 12
Maxheapify
14 13 14 13

11 12 11 12 11 17 11 12

12 11 11 12 14 11

apsort - 24
14, 17, 18, 19, 20, 24, 26

14 11

12 13 12 13
Maxheapify

11 12 11 13 14 12 11 13

12 14 11 12 14 11
13, 14, 17, 18,19, 20, 24, 26
13 11

12 11 12 13
Maxheapify

11 12 11 12 11 17 11 12

12 11 11 12 14 11

apsort - 25
12, 13, 14, 17, 18, 19, 20, 24, 26

12 11
Maxheapify
11 13 12 13

11 12 11 13 14 12 11 13

12 14 11 12 14 11
11, 12, 13, 14, 17, 18,19, 20, 24, 26
11 11
Maxheapify
12 11 12 13

11 12 11 12 11 17 11 12

12 11 11 12 14 11

apsort - 26
Algorithm Analysis
HeapSort(A)
HeapSort(A)
1.
1. Build-Max-Heap(A)
Build-Max-Heap(A)
2. forii
2. for length[A]
length[A]downto
downto22
3.
3. do
doexchange A[1]
exchangeA[1] A[i]
A[i]
4.
4. heap-size[A]
heap-size[A] heap-size[A]
heap-size[A]––11
5.
5. MaxHeapify(A,
MaxHeapify(A,1)1)
 In-place

 Build-Max-Heap takes O(n) and each of the n-1


calls to Max-Heapify takes time O(lg n).

 Therefore, T(n) = O(n lg n)


apsort - 27
Heap Procedures for Sorting
 MaxHeapify O(lg n)
 BuildMaxHeap O(lg n), O(n)?
 HeapSort O(n lg n)

apsort - 28
Thank You

apsort - 29

You might also like