The pseudocode for selection sort is as follows
for int i = 0 to i < n - 1, increment i by 1
{
declare an int named min and set it to i
for int j = i + 1 to j < n, increment j by 1
{
if values[min] > values[j]
{
set min to j
}
}
swap values[i] and values[min]
}
You did most of it correctly though! However, you shouldn't be swapping the two values every time values[min] > values[j]
because that would mess things up. Consider the following example
array: 5, 4, 3, 2, 1
pick the 5
walkthrough the list {4, 3, 2, 1} and pick the least element (i.e., 1)
swap it with the 5
array: 1, 4, 3, 2, 5
...
This way, you guarantee that after the first iteration of the outer loop, the least element in the array will be positioned at index 0.
What you're doing is something like
array: 5, 4, 3, 2, 1
walkthrough the list {4, 3, 2, 1}
if 5 > the current element
swap them
array: 4, 5, 3, 2, 1
...