Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

Selection sort (ascending; in pass i find the minimum of the unsorted part and swap it into position i only if it is not already there) is applied to [27, 5, 47, 17, 3, 34, 46, 41]. What is the array after 3 passes, and how many swaps will the complete sort make?

  1. A. [5, 3, 17, 27, 34, 41, 46, 47]; 5 swaps in total
  2. B. [5, 17, 27, 47, 3, 34, 46, 41]; 5 swaps in total
  3. C. [3, 5, 17, 47, 27, 34, 46, 41]; 5 swaps in total
  4. D. [3, 5, 17, 47, 27, 34, 46, 41]; 7 swaps in total

Correct answer: C. [3, 5, 17, 47, 27, 34, 46, 41]; 5 swaps in total

After 3 passes the 3 smallest values sit in front: [3, 5, 17, 47, 27, 34, 46, 41]. Over the whole sort, 5 passes find their minimum elsewhere and swap, so 5 swaps (at most n − 1).

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

Exams that ask this topic