Data Structures & AlgorithmsVery hardGATE CS · TCS NQT · Product Company SDE
Heap sort (build a max-heap, then repeatedly swap the root with the last unsorted element and sift down) is run on [1, 27, 40, 40, 63, 2, 49, 25, 3]. How many swaps happen in total (build phase plus extraction phase)?
- A. 24
- B. 23
- C. 25
- D. 26
Correct answer: A. 24
Count every swap made while sifting down during build-heap, plus one swap per extraction and any swaps during its sift-down: 24 swaps in total.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A monotonic decreasing stack of indices is used to compute the next-greater-element array for [41, 35, 17, 34, 4, 37, 29, 44] (push index…
- What is the space complexity of this recursive factorial function, as a function of n (ignoring the final integer result size)? c int…
- Evaluate the postfix expression: 6 3 + 7 + 9 +
- Evaluate the postfix expression: 1 1 - 5 - 5 -
- Heap sort (build a max-heap, then repeatedly swap the root with the last unsorted element and sift down) is run on [91, 96, 8, 20, 80, 10…
- Using a monotonic (decreasing) stack to find, for each element of [32, 3, 22, 30, 36, 26, 11, 9], the next element to its right that is…