I am trying to program a quick sort but there seems to be a problem with the code as the list outputted is not sorted but I have checked many implementations and mine is very similar so I do not know the issue.
class QuickSort:
def quickSort(self, list, low, high):
if (low >= high):
return
else:
leftPointer = low
rightPointer = high
pivot = list[high]
while (leftPointer < rightPointer):
while (leftPointer < rightPointer and list[leftPointer] < pivot):
leftPointer += 1
while (leftPointer < rightPointer and list[rightPointer] > pivot):
rightPointer -= 1
list[leftPointer], list[rightPointer] = list[rightPointer], list[leftPointer]
list[high], list[leftPointer] = list[leftPointer], list[high]
self.quickSort(list, low, rightPointer - 1)
self.quickSort(list, rightPointer + 1, high)
return list
list = [50, 49, 19, 4, 9]
quick = QuickSort()
print(quick.quickSort(list, 0, len(list) - 1))
I tried printing the list before the recursive call and found that the list is actually sorted at one point but the returned list is not correct.
Changes: