Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: Selection sort is run on [4, 3, 2, 1] in the usual way (for each position, find the minimum of the unsorted suffix and swap it into place). How many swap operations are performed, counting a swap even when the two indices are equal?

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

Correct answer: A. 3

Selection sort performs one swap per position, whether or not the minimum is already in place. Counting only the swaps that move a value (skipping equal indices) gives a smaller number for sorted prefixes.

More Coding Interview Problems questions

Exams that ask this topic