Suppose we are sorting an array of eight integers using heapsort, and ...
In Heapsort, we first build a heap, then we do following operations till the heap size becomes 1. a) Swap the root with last element b) Call heapify for root c) reduce the heap size by 1. In this question, it is given that heapify has been called few times and we see that last two elements in given array are the 2 maximum elements in array. So situation is clear, it is maxheapify whic has been called 2 times.
View all questions of this test
Suppose we are sorting an array of eight integers using heapsort, and ...
Question Analysis
We are given an array of eight integers and we are using heapsort to sort the array. We are also told that some heapify operations have been performed on the root of the heap. We need to determine how many heapify operations have been performed on the root.
Heapify Operations
Heapify is an important operation in heapsort. It is used to maintain the heap property of a binary heap. There are two types of heapify operations: maxheapify and minheapify.
- Maxheapify: This operation is used to maintain the max heap property. In a max heap, the value of each node is greater than or equal to the values of its children.
- Minheapify: This operation is used to maintain the min heap property. In a min heap, the value of each node is less than or equal to the values of its children.
Array Representation of a Heap
In heapsort, we represent the heap as an array. The root of the heap is at index 0, and its left and right children are located at indices 2i+1 and 2i+2 respectively, where i is the index of the parent node.
Given Array
The given array is: 16 14 15 10 12 27 28
Heapify Operations on Root
To determine the number of heapify operations performed on the root, we need to observe the given array and identify any violations of the heap property at the root.
- In a max heap, the root should have the maximum value among all the elements in the heap. If this is not the case, we need to perform a maxheapify operation to restore the max heap property.
- In a min heap, the root should have the minimum value among all the elements in the heap. If this is not the case, we need to perform a minheapify operation to restore the min heap property.
Observation
Looking at the given array, we can see that the root of the heap is 16. This violates the max heap property because the value of the root should be greater than or equal to the values of its children. In this case, the root has two children, 14 and 15, both of which are smaller than 16.
Heapify Operation
To restore the max heap property, we need to perform a maxheapify operation on the root. This operation will swap the root with its largest child (in this case, 15) and continue recursively until the max heap property is restored.
After performing the maxheapify operation, the array will look like this: 16 15 14 10 12 27 28
Final Analysis
We have performed one maxheapify operation on the root of the heap to restore the max heap property. Therefore, the correct answer is option 'B', which states that two heapify operations have been performed on the root.
To make sure you are not studying endlessly, EduRev has designed Computer Science Engineering (CSE) study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Computer Science Engineering (CSE).