L = [7, 12, 1, -2, 0, 15, 4, 11, 9]
def quicksort(L, low, high):
if low < high:
pivot_location = Partition(L, low, high)
quicksort(L,low, pivot_location)
quicksort(L,pivot_location + 1, high)
return L
def Partition(L, low, high):
pivot = L[low]
leftwall = low
for i in range(low + 1, high, 1):
if L[i] < pivot:
temp = L[i]
L[i] = L[leftwall]
L[leftwall] = temp
leftwall += 1
temp = pivot
pivot = L[leftwall]
L[leftwall] = temp
return leftwall
print(quicksort(L, 0, len(L) - 1))
When I run the code it produces the following result: [-2, 0, 1, 4, 7, 11, 12, 15, 9]. One element is at the wrong position. If anyone could tell me where the problem is ?
highshould belen(L)... And you should split onlow,pivotandpivot,high(so notpivot+1).