Data Structures & AlgorithmsModerateGATE CS
Heap sort (ascending) is run on [3, 13, 29, 24, 7, 25, 19, 40]: build a max-heap bottom-up, then repeatedly swap the root with the last element of the heap part and sift down within the shrunk heap. What is the array after the build phase and exactly 2 extractions?
- A. [29, 24, 25, 13, 7, 3, 19, 40]
- B. [25, 24, 19, 13, 7, 3, 29, 40]
- C. [24, 13, 19, 3, 7, 25, 29, 40]
- D. [40, 24, 29, 13, 7, 25, 19, 3]
Correct answer: B. [25, 24, 19, 13, 7, 3, 29, 40]
Build-heap gives [40, 24, 29, 13, 7, 25, 19, 3]; each extraction parks the current maximum at the end of the array and re-heapifies the prefix, giving [25, 24, 19, 13, 7, 3, 29, 40] after 2 extractions.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Dijkstra's algorithm runs from A on the directed graph A→C (4), A→D (15), B→A (2), B→C (13), C→D (8), C→E (7), C→F (2), D→B (12), E→B (7)…
- Floyd–Warshall runs on vertices 1–4 with directed edges 1→2 (2), 1→3 (8), 2→1 (8), 2→4 (3), 3→2 (7), 3→4 (6), 4→1 (2). After the…
- A tree on vertices 1–9 has edges 2–1, 3–2, 4–2, 5–2, 6–5, 7–4, 8–5, 9–7. What are its diameter and radius, and which vertex/vertices form…
- An undirected graph has edges A–E, A–G, A–H, B–H, C–E, D–F, D–H, E–G, F–H, G–H. BFS starts at A and always scans neighbours in…
- Lectures occupy half-open intervals [start, end): [2, 7), [6, 11), [8, 14), [9, 14), [10, 16), [13, 16), [13, 20), [14, 20). A lecture…
- A binary tree with distinct labels has INORDER D F I E A C H G B and LEVEL-ORDER D G C B E H F A I. What is its postorder traversal?