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)?

  1. A. 24
  2. B. 23
  3. C. 25
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic