In selection sort, we maintain two sub-arrays out of our original array:
the sorted part and the unsorted part.
A = [12,6,5,8,10,3,8,2] this is your initial array
iteration 1:
sorted subarray unsorted subarray
12 6,5,8,10,3,8,2
Result 2 6,5,8,10,3,8,12
we search for the minimum element in our unsorted subarray which is 2. swap 2 with 12 here.
2nd iteration: we will now consider the first element in the unsorted subarray as our second element of sorted array. Note that position of 2 is established because it is the minimum element of the entire array.Again, we search for the smallest element in our unsorted subarray and compare with the second element of sorted subarray. If the former is smaller we swap both of them.
iteration 2:
sorted subarray unsorted subarray
2,6 5,8,10,3,8,12
Result 2,3 5,8,10,6,8,12
There is no element in unsorted subarray smaller than 5 so it stays the same.
so, after 3 iterations the final state would be:
2,3,5,8,10,6,8,12
iteration 3:
sorted subarray unsorted subarray
2,3,5 8,10,3,8,12
Result 2,3,5 8,10,6,8,12