Question text in black, answers in blue.
Use the code from lecture.
findPartition
on page 6 of Lecture #25 is correct. It runs the
while loop until left
and right
are next
to each other, then it swaps the items in locations min
and right
. Shouldn't it instead swap mim
and left
, since min
has to hold a small
element after the partition?
No, I think you are overlooking the fact that
the while loop runs until the condition left < right
is false. You are correct that the leftmost of the two
finger positions, containing the smaller element, should be swapped
with min
. But because left
and
right
have passed one another, that position is now called right
.
Last modified 7 December 2011