Example of Selection Sort (According to Image Values)
Given Array:[5, 3, 8, 4, 1, 12, 7]
Pass 1:
Find smallest in [5, 3, 8, 4, 1, 12, 7] → 1
Swap with first element (5)
Result: [1, 3, 8, 4, 5, 12, 7]
Pass 2:
Find smallest in [3, 8, 4, 5, 12, 7] → 3
Already in correct place
Result: [1, 3, 8, 4, 5, 12, 7]
Pass 3:
Find smallest in [8, 4, 5, 12, 7] → 4
Swap with 8
Result: [1, 3, 4, 8, 5, 12, 7]
Pass 4:
Find smallest in [8, 5, 12, 7] → 5
Swap with 8
Result: [1, 3, 4, 5, 8, 12, 7]
Pass 5:
Find smallest in [8, 12, 7] → 7
Swap with 8
Result: [1, 3, 4, 5, 7, 12, 8]
Pass 6:
Find smallest in [12, 8] → 8
Swap with 12
Result: [1, 3, 4, 5, 7, 8, 12]
Final Sorted Array:
[1, 3, 4, 5, 7, 8, 12]
