GATE Computer ScienceVery hardGATE CS (PYQ pattern)
Heapsort (max-heap, in-place) is applied to [9, 4, 7, 1, 8, 2, 6, 3, 5, 10, 12, 11, 15, 14, 13]. First the array is made a max-heap with Floyd's bottom-up method; then the extract step (swap root with the last unsorted element, shrink the heap, sift down) is performed exactly 4 times. What is the array at that moment?
- A. 11 10 9 5 8 7 6 3 1 4 2 12 13 14 15
- B. 15 14 13 12 2 4 1 3 6 7 8 5 9 10 11
- C. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
- D. 11 10 9 5 8 7 6 3 1 4 2 15 14 13 12
Correct answer: A. 11 10 9 5 8 7 6 3 1 4 2 12 13 14 15
Build-heap gives 15 12 14 5 10 11 13 3 1 4 8 7 2 6 9. Each extraction places the current maximum at the end: after 4 steps the last four cells hold 12, 13, 14, 15 in ascending order and the first 11 cells are a max-heap.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- Keys 50, 30, 70, 20, 40, 60, 80, 10, 25, 35, 45, 55, 65, 75, 85, 5, 12, 22, 27, 90 are inserted in this order into an empty BST (root at…
- How many structurally distinct binary trees (left and right children distinguished) with exactly 7 nodes have exactly 3 leaves?
- A full binary tree has every node with 0 or 2 children; left and right are distinguished. How many structurally different full binary…
- An undirected unweighted graph has edges 1-2, 1-3, 2-4, 2-5, 3-4, 3-6, 4-7, 5-7, 6-7, 6-8, 7-9, 8-9. Using BFS from vertex 1, what is the…
- A directed graph on vertices 1..4 has edges 1->2, 1->3, 2->3, 2->4, 3->1, 3->4, 4->1, 4->2. How many walks of length exactly 4 (edges…
- An undirected graph has 14 vertices, 20 edges and exactly 3 connected components. DFS is restarted from an unvisited vertex until all…