Data Structures & AlgorithmsModerateGATE CS · competitive programming

What is the minimum number of adjacent swaps needed to sort [3, 1, 2, 5, 4] into ascending order?

  1. A. 2
  2. B. 4
  3. C. 5
  4. D. 3

Correct answer: D. 3

Each adjacent swap removes exactly one inversion. This array has three: (3,1), (3,2) and (5,4), so three swaps is the minimum.

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

Exams that ask this topic