Wiki states: "...partitioning algorithm guarantees lo ≤ p < hi which implies both resulting partitions are non-empty, hence there's no risk of infinite recursion."
What prevents Hoare partition from not returning j equal to hi? If our pivot is max number, we may execute j <- j – 1 only once and exit with j=hi
E.g. A= [2,0]
Some implementations add a final swap of placing the initially chosen pivot into returned j:
swap A[lo] with A[j]
return j
What would guarantee p < hi?
Full pseudo-code from wiki:
algorithm partition(A, lo, hi) is
pivot := A[lo]
i := lo - 1
j := hi + 1
loop forever
do
i := i + 1
while A[i] < pivot
do
j := j - 1 <------------- execute once
while A[j] > pivot
if i >= j then
return j <------------- exit
swap A[i] with A[j]