In which iteration does the element '6' get placed at 6th position when selection sort is...
Micron technical mcq question, verified with a worked answer. Free to practise - no sign-up.
In which iteration does the element '6' get placed at 6th position when selection sort is applied on array [9, 6, 1, 4, 3, 2, 7]?
Show answer & explanation
Selection sort iteratively places the smallest remaining element at the front of the unsorted portion. After 4 iterations, elements [1, 2, 3, 4] are sorted in positions 0-3, leaving [9, 6, 7] unsorted. In the 5th iteration, the minimum (6) from the remaining elements is selected and placed at position 4. Element '6' reaches its final sorted position (index 4, or the 5th position when counting from 1) in the 5th iteration.
Step-by-step Derivation:
Trace selection sort on [9, 6, 1, 4, 3, 2, 7]:
Initial: [9, 6, 1, 4, 3, 2, 7]
Iteration 1: Find min in [9, 6, 1, 4, 3, 2, 7] → 1. Swap with index 0.
Result: [1, 6, 9, 4, 3, 2, 7]
Iteration 2: Find min in [6, 9, 4, 3, 2, 7] → 2. Swap with index 1.
Result: [1, 2, 9, 4, 3, 6, 7]
Iteration 3: Find min in [9, 4, 3, 6, 7] → 3. Swap with index 2.
Result: [1, 2, 3, 4, 9, 6, 7]
Iteration 4: Find min in [4, 9, 6, 7] → 4. Swap with index 3.
Result: [1, 2, 3, 4, 9, 6, 7]
Iteration 5: Find min in [9, 6, 7] → 6. Swap with index 4.
Result: [1, 2, 3, 4, 6, 9, 7]
6 is now at index 4 (position 5 when counting from 1). This occurs in the 5th iteration.