if (nums[start] > nums[end])
{
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
}
Since end can iterate down to zero, the comparison can result in swappage in the wrong direction. Here's what happens.
When this algorithm is run, the value 'start' ends up dragging up the least element with it. That is, in the middle of your algorithm, your array might look something like this:
Then, while 'end' iterates downwards, it puts the smallest element between 'start' and 'end' into the position beneath 'start'. In this case, it swaps 5 with 7, and no more swaps are made.
Then 'end' reaches the position just before 'start'. Since nums[start] > nums[end], the two are swapped.
2
3
4
5 end (after swapping)
1 start
6
8
7
9
Then 'end' reaches -1 with no further swapping (because nums[start] contains the minimum element), leaving the beginning region sorted. Then the loop restarts, with 'start' incremented.
And it happens all over again...