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 ...
Explanation:
Heapify is the process of creating a heap from an array. It is used as a subroutine in sorting algorithms like heapsort. In this question, we are given an array of eight integers: 16 14 15 10 12 27 28. We are also told that some heapify operations (either maxheapify or minheapify) have been performed on it.
Step 1: Identify the root of the heap
The root of the heap is the first element of the array, which is 16.
Step 2: Check if the root satisfies the heap property
A heap is said to satisfy the heap property if it is either a max heap or a min heap. In a max heap, the value of any node is greater than or equal to the values of its children. In a min heap, the value of any node is less than or equal to the values of its children.
If the root satisfies the heap property, then no heapify operation is needed on it. Otherwise, we need to perform a heapify operation to restore the heap property.
Step 3: Perform heapify operation on the root
In this case, we can see that the root does not satisfy the max heap property, as its value is less than the values of its children (27 and 28). Therefore, we need to perform a maxheapify operation on the root.
After performing maxheapify on the root, the array looks like this: 28 14 27 10 12 15 16. We can see that the new root is 28, which satisfies the max heap property.
Step 4: Check if the new root satisfies the heap property
We need to check if the new root (28) satisfies the max heap property. In this case, it does, because its value is greater than or equal to the values of its children (14 and 27).
Therefore, we have performed one heapify operation on the root of the heap.
Step 5: Repeat steps 2-4 until the entire array is a heap
We need to repeat steps 2-4 until the entire array is a heap. In this case, we can see that the entire array is already a heap, because every node satisfies the max heap property.
Therefore, the total number of heapify operations performed on the root of the heap is 1.
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).