I translated the Hoare partition scheme from a Wikipedia article to Python:
Here's my code:
def partition(nums, low, high):
pivot = nums[(high + low) // 2]
i = low - 1
j = high + 1
while True:
i += 1
j -= 1
while nums[i] < pivot:
i += 1
while nums[j] > pivot:
j -= 1
if i >= j:
return j
nums[i], nums[j] = nums[j], nums[i]
nums = [14801, 338, 6389, 3209, 4825, 10818, 1768, 7669, 4545, 10930, 11810]
pivot_1 = partition(nums, 0, len(nums) - 1)
print(pivot_1) # 6 ---> this should be 7, no?
print(nums) # [4545, 338, 6389, 3209, 4825, 7669, 1768, 10818, 14801, 10930, 11810]
pivot_2 = partition(nums, 0, 6)
print(pivot_2) # 2 ---> this looks ok
print(nums) # [1768, 338, 3209, 6389, 4825, 7669, 4545, 10818, 14801, 10930, 11810]
What am I doing wrong? Why is my code not returning the correct pivot location?

whileloops test and if false don’t execute the increment/decrement, where the wikipedian equivalent seem to always increment/decrement before doing the test?while nums[i]<pivot:will not execute the contained increment if the test is false, whereas the wikipedian will incrementibefore testing