Lecture Six-Algorithms and Problem Solving
Lecture Six-Algorithms and Problem Solving
Lecture 6: Heapsort
Rawand Al-Foqah’a
2
Binary Tree DATA STRUCTURE
3
HEAP DATA STRUCTURE
➢ A heap data structure is an array
object that we can view as a nearly
complete binary tree.
❑ Each node of the tree
corresponds to an element of the
array.
❑ The tree is completely filled on
all levels except possibly the
lowest, which is filled from the
left up to a point.
4
HEAP DATA STRUCTURE
➢ An array A[1:n] that represents a heap
is an object with an attribute A.heap-
size.
➢ Represents how many elements in
the heap are stored within array A.
➢ Although A[1:n] may contain
numbers, only the elements in A[1:
A.heap-size], where
0 <= A.heap-size<= n, are valid
elements of the Heap.
➢ If A.heap-size =0, then the heap is
empty.
5
HEAP DATA STRUCTURE
➢ Height of node = # of edges on a
longest simple path from the
node down to a leaf.
▪ Height of heap = height of root = Θ(lgn)
▪ Example: Of a max-heap in array with
heap – size=10
6
Max-heap & Min-heap property
7
HEAP DATA STRUCTURES (continued)
8
EXAMPLE
• Max-heap with heap-size =10 viewed as:
(a) a binary tree
(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.
9
EXAMPLE
10
MAINTAINING THE HEAP PROPERTY
MAX-HEAPIFY is important for manipulating max-heaps. It is used to
maintain the max-heap property.
• Before MAX-HEAPIFY, 𝐴[𝑖] may be smaller than its children.
• Assume that left and right subtrees of 𝑖 are max-heaps.
• (No violations of maxheap property within the left and right
subtrees. The only violation within the subtree rooted at 𝑖 could be
between 𝑖 and its children.)
• After MAX-HEAPIFY, subtree rooted at 𝑖 is a max-heap.
11
PSEUDOCODE
12
THE WAY MAX-HEAPIFY WORKS
• Compare 𝐴[𝑖], 𝐴[𝐿𝐸𝐹𝑇(𝑖)], and 𝐴[𝑅𝐼𝐺𝐻𝑇(𝑖)].
• If necessary, swap 𝐴[𝑖] with the larger of the two children to preserve
heap property.
• Continue this process of comparing and swapping down the heap,
until subtree rooted at 𝑖 is max-heap. If we hit a leaf, then the subtree
rooted at the leaf is trivially a max-heap.
13
EXAMPLE
Run MAX-HEAPIFY on the following heap example.
15
EXAMPLE
Building a max-heap by calling BUILD-MAX-HEAP(𝐴, 10) on the
following unsorted array array 𝐴 1: 10 results in the first heap
example.
16
EXAMPLE (continued)
17
THE HEAPSORT ALGORITHM
Given an input array, the heapsort algorithm acts as follows:
• Builds a max-heap from the array.
• Starting with the root (the maximum element), the algorithm places
the maximum element into the correct place in the array by swapping
it with the element in the last position in the array.
• “Discard” this last node (knowing that it is in its correct place) by
decreasing the heap size, and calling MAX-HEAPIFY on the new
(possibly incorrectly-placed) root.
• Repeat this “discarding” process until only one node (the smallest
element) remains, and therefore is in the correct place in the array.
18
PSEUDOCODE
19
EXAMPLE
Sort an example heap on the board. [Nodes with heavy outline are no
longer in the heap.]
20
Heapsort running time
• Worse case tuning time is 𝑂(𝑛 lg 𝑛) like merge sort.
• Sort in place like insertion sort.
• Combines the best of both algorithms.
• Though heap sort is great algorithm, a well implemented quick sort
usually beats it in practice.
21
End Of the Lecture
23