And if you find the pivot...it get's switched? I thought that's the point of quicksort, so it leads to the proper position of the pivot element. |
The array should end
[ Lesser | Pivot | Greater ] (it could be relaxed to
[ <= | Pivot | >= ] )
Suppose this sequence 1 5 2 3 4, pivot is 3.
It will end 1 3 2 5 4. Note that there is an smaller value to the right of the pivot, that is incorrect.
A way to handle it is to send the pivot "outside" the array, and when you finish the partition put it in the right position
1 5 2 3 4 -> 1 5 2 4 (3) (the array now it's just 1 5 2 4)
1 5 2 4 (3)
1 2 5 4 (3)
And now swap it to the right position 1 2 (3) 4 5
Unless you mean the pointer values, in which case the entire loops cancels. |
Yes, I mean the positions. Nope, it cancels one step too late
Suppose this sequence ... 1 3 42 54 ... (left points to 1, right points to 54, pivot is 13)*
left advances till it tops with 42.
right advances till it tops with 3.
Now the partition should end, however:
You swap the values -> ... 1 42 3 54 ... Then you break
*As you can see the pivot is not in the extract of the sequence. That could happen because you swapped before, or you send "outside" the array