Quicksort partition algorithm -- why is the swap with the pivot value outside of the loop?

31 Views Asked by At

I was looking at the quick sort algorithm in CLRS's Introduction to Algorithms https://dl.ebooksworld.ir/books/Introduction.to.Algorithms.4th.Leiserson.Stein.Rivest.Cormen.MIT.Press.9780262046305.EBooksWorld.ir.pdf

On pg 184, it shows the partition algorithm, pasted below for convenience:

enter image description here

It's been many years since I've looked at this, but now I don't understand why we need line 7 as opposed to make the loop go from j = p to j = r, and return i. Is there some performance improvement to doing it as shown in the picture?

0

There are 0 best solutions below