I'm trying to write add some features to an implementation of Hoare's paritioning scheme for quicksort. I have what I believe should be a working function but I'm getting a runtime error:
TypeError: cannot unpack non-iterable int object
Can anyone help me understand what's going on and how to fix it? Thank you!!
def partition(arr, low, high):
comp = 0
swap = 0
pivot = arr[low]
i = low - 1
j = high + 1
while (True):
# Find leftmost element greater than
# or equal to pivot
i += 1
while (arr[i] < pivot):
comp += 1
i += 1
# Find rightmost element smaller than
# or equal to pivot
j -= 1
while (arr[j] > pivot):
comp+=1
j -= 1
# If two pointers met.
if (i >= j):
return j
swap += 1
arr[i], arr[j] = arr[j], arr[i]
def quicksort(A, lo, hi):
if lo >= 0 and hi >= 0 and lo < hi:
(comp, swap, p) = partition(A, lo, hi)
(lcomp, lswap) = quicksort(A, lo, p)
(rcomp, rswap) = quicksort(A, p + 1, hi)
return (comp + lcomp + rcomp, swap + lswap + rswap)
return (0,0)
def printArray(arr, n):
for i in range(n):
print(arr[i], end=" ")
print()
# Driver code
arr = [2,8,7,1,3,5,6,4]
n = len(arr)
print(quicksort(arr, 0, len(arr)-1))
print(arr)
I've been trying to wrap my head around why it doesn't work and have looked at similar code and just don't see what I'm missing. I'm relatively new to Python so please forgive me if this is a simple fix.