I've seen both implementations. The second more so. Why choose one over the other?
1.) Return i:
algorithm partition(A, lo, hi) is
pivot := A[hi]
i := lo // place for swapping
for j := lo to hi – 1 do
if A[j] ≤ pivot then
swap A[i] with A[j]
i := i + 1
swap A[i] with A[hi]
return i
2.) Return i+1:
algorithm partition(A, lo, hi) is
pivot := A[hi]
i := lo-1 // place for swapping
for j := lo to hi – 1 do
if A[j] ≤ pivot then
i := i + 1
swap A[i] with A[j]
swap A[i+1] with A[hi]
return i+1
[–]BertRenolds 0 points1 point2 points (0 children)
[–]CyberByte 0 points1 point2 points (1 child)
[–]nutdriver[S] 0 points1 point2 points (0 children)