0% found this document useful (0 votes)
11 views82 pages

Lecture 12 - Heap Sort

The document provides an overview of heap sort, a sorting algorithm that utilizes the heap data structure, characterized as a complete binary tree with a specific order property. It explains the concepts of max heaps and min heaps, detailing how elements are inserted and deleted while maintaining the heap property. Additionally, it includes examples and algorithms for insertion and deletion operations in heaps.

Uploaded by

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

Lecture 12 - Heap Sort

The document provides an overview of heap sort, a sorting algorithm that utilizes the heap data structure, characterized as a complete binary tree with a specific order property. It explains the concepts of max heaps and min heaps, detailing how elements are inserted and deleted while maintaining the heap property. Additionally, it includes examples and algorithms for insertion and deletion operations in heaps.

Uploaded by

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

CS-251 – Design and Analysis of Algorithms

Instructor: Dr. Zuhair Zafar


Lecture # 12: Heap Sort

1
Recap: Types of Sorting Algorithms
• Non-recursive/Incremental comparison sorting
• Selection sort
• Bubble sort
• Insertion sort
• Recursive comparison sorting
• Merge sort
• Quick sort
• Heap sort
• Non-comparison linear sorting
• Count sort
• Radix sort
2
• Bucket sort
What is a Heap?
A heap is a data structure that stores a collection of objects (with
keys), and has the following properties:

• Complete Binary tree


• Heap Order

It is implemented as an array where each node in the tree


corresponds to an element of the array.

3
What is a Complete Binary Tree?

• All the levels in the tree are completely filled except the last level.

• In the last level, all the keys are assigned as left as possible.

How can we represent an array in a binary tree format?

4
What is a Complete Binary Tree?

• All the levels in the tree are completely filled except the last level.

• In the last level, all the keys are assigned as left as possible.

How can we represent an array in a binary tree format?

1 2 3 4 5 6
8 7 10 1 5 9
5
Array A
What is a Complete Binary Tree?

• All the levels in the tree are completely filled except the last level.

• In the last level, all the keys are assigned as left as possible.

How can we represent an array in a binary tree format?

1
8

1 2 3 4 5 6
8 7 10 1 5 9
6
Array A
What is a Complete Binary Tree?

• All the levels in the tree are completely filled except the last level.

• In the last level, all the keys are assigned as left as possible.

How can we represent an array in a binary tree format?

1
8
2
7 1 2 3 4 5 6
8 7 10 1 5 9
7
Array A
What is a Complete Binary Tree?

• All the levels in the tree are completely filled except the last level.

• In the last level, all the keys are assigned as left as possible.

How can we represent an array in a binary tree format?

1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
8
Array A
What is a Complete Binary Tree?

• All the levels in the tree are completely filled except the last level.

• In the last level, all the keys are assigned as left as possible.

How can we represent an array in a binary tree format?

1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 9
1 Array A
What is a Complete Binary Tree?

• All the levels in the tree are completely filled except the last level.

• In the last level, all the keys are assigned as left as possible.

How can we represent an array in a binary tree format?

1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 10
1 5 Array A
What is a Complete Binary Tree?

• All the levels in the tree are completely filled except the last level.

• In the last level, all the keys are assigned as left as possible.

How can we represent an array in a binary tree format?

1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 11
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:

• PARENT 𝑖 • LEFT_CHILD 𝑖 • RIGHT_CHILD 𝑖


return 𝑖Τ2 return 2𝑖 return 2𝑖 + 1

1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 12
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:

• PARENT 𝑖 • LEFT_CHILD 𝑖 • RIGHT_CHILD 𝑖


return 𝑖Τ2 return 2𝑖 return 2𝑖 + 1

1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 13
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:

• PARENT 𝑖 • LEFT_CHILD 𝑖 • RIGHT_CHILD 𝑖


return 𝑖Τ2 return 2𝑖 return 2𝑖 + 1

PARENT (3)
1 return 3/2 = 1.5 = 1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 14
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:

• PARENT 𝑖 • LEFT_CHILD 𝑖 • RIGHT_CHILD 𝑖


return 𝑖Τ2 return 2𝑖 return 2𝑖 + 1

PARENT (3)
1 return 3/2 = 1.5 = 1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 15
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:

• PARENT 𝑖 • LEFT_CHILD 𝑖 • RIGHT_CHILD 𝑖


return 𝑖Τ2 return 2𝑖 return 2𝑖 + 1

1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 16
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:

• PARENT 𝑖 • LEFT_CHILD 𝑖 • RIGHT_CHILD 𝑖


return 𝑖Τ2 return 2𝑖 return 2𝑖 + 1

LEFT_CHILD (3)
1 return 2(3) = 6
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 17
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:

• PARENT 𝑖 • LEFT_CHILD 𝑖 • RIGHT_CHILD 𝑖


return 𝑖Τ2 return 2𝑖 return 2𝑖 + 1

LEFT_CHILD (3)
1 return 2(3) = 6
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 18
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:

• PARENT 𝑖 • LEFT_CHILD 𝑖 • RIGHT_CHILD 𝑖


return 𝑖Τ2 return 2𝑖 return 2𝑖 + 1

1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 19
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:

• PARENT 𝑖 • LEFT_CHILD 𝑖 • RIGHT_CHILD 𝑖


return 𝑖Τ2 return 2𝑖 return 2𝑖 + 1

RIGHT_CHILD (2)
1 return 2 2 + 1 = 5
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 20
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:

• PARENT 𝑖 • LEFT_CHILD 𝑖 • RIGHT_CHILD 𝑖


return 𝑖Τ2 return 2𝑖 return 2𝑖 + 1

RIGHT_CHILD (2)
1 return 2 2 + 1 = 5
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 21
1 5 9 Array A
What is a Heap Order Property?
• For every node 𝑖, other than the root, the key stored in 𝑖 is greater
or equal than the key stored in the parent of 𝑖. (Case for Min Heap)

• For every node 𝑖, other than the root, the key stored in 𝑖 is smaller
or equal than the key stored in the parent of 𝑖. (Case for Max Heap)

• Max Heap
• Store data in ascending order
• Has property of
𝐴[𝑃𝑎𝑟𝑒𝑛𝑡(𝑖)] ≥ 𝐴[𝑖]

• Min Heap
• Store data in descending order
• Has property of 22
𝐴[𝑃𝑎𝑟𝑒𝑛𝑡(𝑖)] ≤ 𝐴[𝑖]
Max Heap Example

19

12 16

1 4 7

19 12 16 1 4 7

Array A 23
Min Heap Example

4 16

7 12 19

1 4 16 7 12 19

Array A 24
Heap Property
20
Larger than
its parent
15 8

4 10 7 9
Not a Heap

25

Not left-justified 10 12

9 5 11 7
25

1 6 3 8
Insertion of Key in a Heap
Algorithm

1. Add the new element to the next available position at the lowest
level
2. Restore the max-heap property if violated
 General strategy is percolate up (or bubble up): if the parent of the
element is smaller than the element, then interchange the parent and
child.

OR

Restore the min-heap property if violated


 General strategy is percolate up (or bubble up): if the parent of the
element is larger than the element, then interchange the parent and
child.
26
Insertion of Key in a Max Heap
19 19

12 16 12 16

4 7 1 4 7 17
1
Insert 17
19

12 17
swap
27

4 7 16 Filter gradually up to
1
maintain the heap property
Deletion of Key in a Heap
Delete max
• Copy the last number to the root (overwrite the maximum element
stored there).
• Restore the max heap property by percolate down (filter down).

Delete min
• Copy the last number to the root (overwrite the minimum element
stored there).
• Restore the min heap property by percolate down (filter down).

28
Deletion of Key in a Max Heap
19
swap
delete 19

12 16 12 16

4 7 1 4 7
1

7 16
swap

12 16 12 7

29
Filter gradually
4 4 down to maintain
1 1
the heap property
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n)
1. l ← LEFT_CHILD(i)
2. r ← RIGHT_CHILD(i)
3. if l ≤ n and A[l] > A[i]
4. then largest ←l
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 30
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 𝑖=1


1. l ← LEFT_CHILD(i) l=2, r=3, n=6 4
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 14 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 31
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 𝑖=1


1. l ← LEFT_CHILD(i) l=2, r=3, n=6 4
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 14 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 32
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 𝑖=1


1. l ← LEFT_CHILD(i) l=2, r=3, n=6 4
largest = 2
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 14 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 33
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 𝑖=1


1. l ← LEFT_CHILD(i) l=2, r=3, n=6 4
largest = 2
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 14 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 34
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 𝑖=1


1. l ← LEFT_CHILD(i) l=2, r=3, n=6 4
largest = 2
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 14 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 35
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 𝑖=1


1. l ← LEFT_CHILD(i) l=2, r=3, n=6 14
largest = 2
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 36
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 𝑖=1


1. l ← LEFT_CHILD(i) l=2, r=3, n=6 14
largest = 2
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 37
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=?, r=?, n=6 14
largest = ?
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 38
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = ?
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 39
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = ?
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 40
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = 2
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 41
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = 2
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 42
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 43
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 44
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 8 7
5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 45
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 8 7
5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 46
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=?, r=?, n=6 14
largest = ?
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
i=5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 47
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=10, r=11, n=6 14
largest = ?
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
i=5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 48
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=10, r=11, n=6 14
largest = ?
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
i=5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 49
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=10, r=11, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
i=5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 50
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=10, r=11, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
i=5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 51
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=10, r=11, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
i=5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] A[largest] 52
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children

Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) 14
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r Complexity of MAX-HEAPIFY()?
8. if largest  i
9. then exchange A[i] A[largest] O(lg n) where lg n is the
53
10. MAX-HEAPIFY(A, largest, n) height of the tree
Array Representation of Heaps
A heap can be stored as an array A.
• Root of tree is A[1] 1 2 3 4 5 6 7 8 9 10
26 24 20 18 17 19 13 12 14 11
• Left child of A[i] = A[2i]
Max-heap as an array
• Right child of A[i] = A[2i + 1]
• Parent of A[i] = A[i/2] 26
• Heapsize[A] ≤ length[A]
24 20
The elements in the subarray
A[(n/2+1) .. n] are leaves
18 17 19 13

Max-heap as a binary tree


12 14 11 54

Node Insertion: Insert in last row from left to right


Node Deletion: Delete from last row from right to left
Building a Heap
A given array containing n elements can be converted into a max-heap
by applying MAX-HEAPIFY on all interior nodes
• The elements in the subarray A[1 … n/2] are interior nodes
• The elements in the subarray A[(n/2+1) .. n] are leaves

Alg: BUILD-MAX-HEAP(A)
1. n = length[A]
2. for i ← n/2 downto 1
O(n)
3. do MAX-HEAPIFY(A, i, n) O(lgn)
1

4
Complexity of BUILD-MAX-HEAP? O(n lgn)
2 3

1 3 55
4 5 6 7
A: 4 1 3 2 16 9 10 14 8 7 2 16 9 10
8 9 10

14 8 7
Example: A: 4 1 3 2 16 9 10 14 8 7

i=5 i=4 i=3


1 1 1

4 4 4
2 3 2 3 2 3

1 3 1 3 1 3
4 5 6 7 4 5 6 7 4 5 6 7

8
2 9 10
16 9 10 8
2 9 10
16 9 10 8
14 9 10
16 9 10
14 8 7 14 8 7 2 8 7

i=2 i=1
1 1 1

4 4 16
2 3 2 3 2 3

1 10 16 10 14 10
4 5 6 7 4 5 6 7 4 5 6 7

8
14 9 10
16 9 3 8
14 9 10
7 9 3 8
8 9 10
7 9 3
2 8 7 2 8 1 2 4 1
56
Heap Sort
A given array which has been heapified does not mean it is sorted

25

22 17

14 21 9 11

Notice that the largest number is at the root. It can be swapped


with the rightmost leaf at the deepest level (its original position)
and heap size decreased.

The new root has lost heap property. Heapify the new root and
repeat this process until only one node remain. 57
Example: A=[7, 4, 3, 1, 2]

MAX-HEAPIFY(A, 1, 4) MAX-HEAPIFY(A, 1, 3) MAX-HEAPIFY(A, 1, 2)

MAX-HEAPIFY(A, 1, 1) 58
Heap Sort Pseudocode
HEAPSORT(A)
1. BUILD-MAX-HEAP(A) O(n lgn)
2. for i ← length[A] downto 2
3. do exchange A[1] A[i] n-1 times
4. MAX-HEAPIFY(A, 1, i - 1) O(lgn)
What is the running time of HEAPSORT()? O(n lgn)

Why Heap Sort?


• Heap Sort is always O(n lgn) and is in-place
• Quicksort is usually O(n lgn) but in the worst case slows to O(n2)
• Quicksort is generally faster, but Heapsort is better in time-critical 59
applications
Example
Example: Sort the following array using heap sort

19 12 16 1 4 7

Picture the array as a complete binary tree:

19

12 16

60
1 4 7
Example
Take out biggest
19

12 16
Move the last element
to the root

1 4 7

Sorted:
Array A

12 16 1 4 7 19
61

7 12 16 1 4 19
Example

7
swap
HEAPIFY()
12 16

1 4

Sorted:
Array A

7 12 16 1 4 19
62

7 12 16 1 4 19
Example

16

12 7

1 4

Sorted:
Array A

16 12 7 1 4 19
63

16 12 7 1 4 19
Example
Take out biggest
16
Move the last element
to the root
12 7

1 4

Sorted:
Array A

12 7 1 4 16 19
64

4 12 7 1 16 19
Example
4

12 7

Sorted:
Array A

4 12 7 1 16 19
65

4 12 7 1 16 19
Example

swap 4

HEAPIFY()
12 7

Sorted:
Array A

4 12 7 1 16 19
66

4 12 7 1 16 19
Example

12

4 7

Sorted:
Array A

12 4 7 1 16 19
67

12 4 7 1 16 19
Example
Take out biggest
12
Move the last
element to the
root 4 7

Sorted:
Array A

4 7 1 12 16 19
68

1 4 7 12 16 19
Example

1
swap

4 7

Sorted:
Array A

1 4 7 12 16 19
69

1 4 7 12 16 19
Example

4 1

Sorted:
Array A

7 4 1 12 16 19
70

7 4 1 12 16 19
Example
Take out biggest
7
Move the last
element to the
4 1 root

Sorted:
Array A

1 4 7 12 16 19
71

1 4 7 12 16 19
Example

swap 1

HEAPIFY()
4

Sorted:
Array A

1 4 7 12 16 19
72

1 4 7 12 16 19
Example
4

Sorted:
Array A

4 1 7 12 16 19
73

4 1 7 12 16 19
Example
Take out biggest
Move the last 4
element to the
root
1

Sorted:
Array A

1 4 7 12 16 19
74

1 4 7 12 16 19
Example
Take out biggest
1

Sorted:
Array A

1 4 7 12 16 19
75

1 4 7 12 16 19
Example

Sorted:

1 4 7 12 16 19

76
Priority Queues
Priority Queue is an extension of queue with
following properties.
⚫ Every item has a priority associated with it.

⚫ An element with high priority is dequeued before


an element with low priority.
⚫ If two elements have the same priority, they are
served according to their order in the queue.
Priority Queues
A max-Priority Queue supports the following
operations:
⚫ Maximum (A) returns the element of A with
the largest key.
⚫ Extract-Max (A) removes and returns the
element of A with the largest key.
⚫ Increase-Key (A, i, key) increases the value
of element i’s key to the new value key
⚫ Insert (A, key) inserts the key into the set A
Priority Queues
⚫ Maximum (A) returns the element of A with
the largest key.
Priority Queues
⚫ Extract-Max (A) removes and returns the
element of A with the largest key.
Priority Queues
⚫ Increase-Key (A, i, key) increases the value
of element ‘i’ to the new value key
Priority Queues
⚫ Insert (A, key) inserts the key into the set A

You might also like