Data Structures & AlgorithmsVery hard

Selection sort (ascending) is run on 7 2 9 4 1 6, swapping the minimum of the unsorted part into place in each pass, and skipping the swap when the minimum is already in place. How many swaps are performed?

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

Correct answer: B. 3

Selection sort performs at most n−1 swaps, and only when the chosen minimum is not already at the front of the unsorted part. Counting inversions measures a different quantity, and the n−1 bound is reached only in special cases.

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

Exams that ask this topic