Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2 Heaps and Heapsort (25 points) 2.1 Is the sequence a max-heap? Why or why not?

ID: 3889354 • Letter: 2

Question

2 Heaps and Heapsort (25 points) 2.1 Is the sequence a max-heap? Why or why not? 2.2 Illustrate the operation of Max-HEAPIPY(A; 3) on the array A27, 17,3, 16, 13, 10, 1,5, 7,12,4,8,9,10> using the binary trees as we did in class. 2.3 Illustrate the operation of BUILD-MAX-HEAP(A) on the array A =, using the binary trees as we did in class 2.4 You are given a list of numbers for which you need to construct a min-heap. How would you use an algorithm for constructing a max-heap to construct a min-heap? Explain in your own words. 2.5 Argue the correctness of Heapsort using the following loop invariant: At the start of each iteration of the for loop, the subarray Al,.. ,i] is a max-heap containing the i smallest elements of A[1,...,n], and the subarray Ait 1, . . . 'n contains the n-largest elements of Al' ·…

Explanation / Answer

Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for remaining element.

What is Binary Heap?
Let us first define a Complete Binary Tree. A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible (Source Wikipedia)

A Binary Heap is a Complete Binary Tree where items are stored in a special order such that value in a parent node is greater(or smaller) than the values in its two children nodes. The former is called as max heap and the latter is called min heap. The heap can be represented by binary tree or array.

Why array based representation for Binary Heap?
Since a Binary Heap is a Complete Binary Tree, it can be easily represented as array and array based representation is space efficient. If the parent node is stored at index I, the left child can be calculated by 2 * I + 1 and right child by 2 * I + 2 (assuming the indexing starts at 0).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote