posted 10 years ago
No, a selection sort is just a slightly more optimal bubble sort that requires O(N) swaps instead of O(N²), but still requires O(N²) comparisons. It does this by iterating the entire remainder of the array relative to the starting index and "selecting" the index of the smallest value it encounters. Only after the entire remainder of the array has been iterated are values at the starting index and the "smallest value" index finally swapped. So, in your example, the inner loop would perform 5 iterations, discover (coincidentally during the fifth iteration) that 0 is the smallest value in the array and set min to point at the index of that smallest value; that index is 5. The inner loop then completes. Only now, in the first iteration of the outer loop, are the values at index 0 and index 5 actually swapped, after which the outer loop starts its second iteration. Long story short, no, 0 does not slowly work its way to the start of the array, it is selected into that position immediately.
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.