Yes, it computes the correct value, although it may needlessly examine pairs after it has already found one out of order.
Notice that the >
operator is correctly used. Using >=
would not work.
Here is another list of integers:
1. Select the smallest integer in this list and move it into the left-most position.
The original left-most integer is moved into the original location of the integer you moved. (This is different than the previous demonstrations, which behaved more like playing cards.)
After doing this, the left-most single integer is a sorted sub-list.
2. Next, select the smallest integer from the unsorted integers on the right and move it just to the right of the left-most integer.
Now the left two integers are a sorted sub-list.
3. Next, select the smallest integer from the unsorted integers on the right and move it just to the right of the sorted sub-list
Now the left three integers are a sorted sub-list.
Keep selecting the smallest integer from the unsorted right part of the list and placing it just to the right of the sorted sub-list on the left. Soon, the sub-list on the left contains all the integers, which are now sorted. (Click "refresh" in your browser if you want to do this again.)
The idea is to keep growing the sorted list on the left by selecting the smallest integer from the right. Soon, the list of the left contains all integers in sorted order.
This sounds suspiciously like an algorithm for sorting. What do you suppose the algorithm is called?