Problem 346389 · easy · Level 03 Linear Management & Searching

Selection Sort Swap Log

sorting · selection sort · simulation

Implement selection sort and return the list of swaps it performs. For each position i from 0 to n-2, find the index m of the smallest value in nums[i:] (if the smallest value appears more than once, take the leftmost occurrence). If m != i, swap nums[i] and nums[m] and record the tuple (i, m). If m == i, record nothing.

Return the list of recorded tuples in the order they happened. Do not modify the input.

Examples

Input:  nums = [3, 1, 2]
Output: [(0, 1), (1, 2)]
Explanation: Swap positions 0 and 1 -> [1, 3, 2]; swap positions 1 and 2 -> [1, 2, 3].
Input:  nums = [2, 2, 1]
Output: [(0, 2)]
Explanation: After swapping 0 and 2 the list is [1, 2, 2]; position 1 already holds the minimum.

Constraints

  • 0 <= len(nums) <= 500
  • Target complexity: O(n^2).

Goals

  • Implement selection sort with an explicit minimum search
  • Log only the swaps that actually change the list
  • Pick the first occurrence when the minimum is tied
Starting Python…