The outer loop goes through all the positions in that array.
For each step of the outer loop, the inner loop finds the value that belongs in the position selected by the outer loop. Then the swap function is used to swap the value that was in that position with the value that belongs in that position.
When looking for the value that belongs in a position, you obviously do not want to consider the smaller values that have already been placed in their final location earlier in the array. Since you don't consider those, the value that should be placed in the current position is always the smallest value that has not yet been moved to its final position.
In your example it would
Find the smallest of 15, 32, 8, 64, 82 and swap that with the 15 to get 8, 32, 15, 64, 82
Find the smallest of 32, 15, 64, 82 and swap that with the 32 to get 8, 15, 32, 64, 82
Find the smallest of 32, 64, 82 and swap that with the 32, which changes nothing.
Find the smallest of 64, 82 and swap that with the 64, which changes nothing.
Done.
Last edited by johnsfine; 03-08-2011 at 09:22 AM.
|