0% found this document useful (0 votes)
2 views

Lecture Six-Algorithms and Problem Solving

The document provides an overview of heaps and the heapsort algorithm, detailing the structure and properties of heaps, including max-heaps and min-heaps. It explains the MAX-HEAPIFY process for maintaining heap properties and outlines the steps involved in the heapsort algorithm, which includes building a max-heap and sorting the elements. The document concludes with a comparison of heapsort's performance to other sorting algorithms, noting its worst-case time complexity of O(n log n).

Uploaded by

Omnia barakat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture Six-Algorithms and Problem Solving

The document provides an overview of heaps and the heapsort algorithm, detailing the structure and properties of heaps, including max-heaps and min-heaps. It explains the MAX-HEAPIFY process for maintaining heap properties and outlines the steps involved in the heapsort algorithm, which includes building a max-heap and sorting the elements. The document concludes with a comparison of heapsort's performance to other sorting algorithms, noting its worst-case time complexity of O(n log n).

Uploaded by

Omnia barakat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Algorithms and Problem Solving

Course Number: ITCC103

Lecture 6: Heapsort

Rawand Al-Foqah’a

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 1


Guiding Textbook
These slides are formatted and structured based on the content from the following
reference book:

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)

The root of the tree is A[1]


and given the index i of a node,
there’s a simple way to compute
the indices of its parent, left child,
and right child with the one-line
procedures PARENT, LEFT, and RIGHT.

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.

Time: 𝑂(lg 𝑛).


14
BUILDING A HEAP
The following procedure, given an unordered array 𝐴[1: 𝑛], will
produce a max-heap of the 𝑛 elements in 𝐴.

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

COLLEGE OF INFORMATION TECHNOLOGY - LUSAIL UNIVERSITY 22


References
❑Thomas H. CORMEN, et al. Introduction to Algorithms. The MIT
Press, Cambridge, Massachusetts London, England, ISBN
9780262046305, 2022.

23

You might also like