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 [91, 96, 8, 20, 80, 10, 98, 83]. How many swaps happen in total (build phase plus extraction phase)?

  1. A. 21
  2. B. 20
  3. C. 19
  4. D. 18

Correct answer: C. 19

Count every swap made while sifting down during build-heap, plus one swap per extraction and any swaps during its sift-down: 19 swaps in total.

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

Exams that ask this topic