Lecture 6 Algorithms and Problem Solving
Lecture 6 Algorithms and Problem Solving
Learning outcomes
At the end of this lecture you should Have:
Understood what is a Heap data structure.
Learned how to build a heap and how to maintain Max-heap property.
Understood how the Heapsort algorithm operates.
Learned about Quicksort algorithm and how it follows the divide and conquer
method.
Siblings
Leaf node
Child
College of Information Technology
Lusail University 4
Example
Max-heap with heap-size =10 viewed as:
(a) a binary tree and
(b) an array.
The number within the circle at each node in the tree
is the value stored at that node.
The number above a node is the corresponding index
in the array.
Above and below the array are lines showing parent-
child relationships, with parents always to the left of
their children.
The tree has height 3
The node at index 4 (with value 8) has height 1.
College of Information Technology
Lusail University 10
Pseudocode
Example
• Run MAX-HEAPIFY on the following heap example.
Building a Heap
The following procedure, given an unordered array [1: ], will
produce a max-heap of the elements in .
Example
Building a max-heap by calling BUILD-MAX-HEAP( , 10) on the
following unsorted array [1: 10] results in the first heap example.
Example Cont.
Pseudocode
Quicksort