So my algorithm has two functions. The first is partition which takes the first element of the list as the pivot and places all elements larger than it after it and all the smallest before it and I have tested it and it works just fine. The second function quicksort function and it uses partition recursively to sort the entire list. When I run the code in pycharm it says maximum recursion depth exceeded and a few other errors. Here is the code:
arr = [81, 4, 73, 1, 98, 69, 300, 14, 7, 420, 190, 8, 9]
def partition(low, high):
lo = low
hi = high
pivot = arr[lo]
while lo < hi:
while arr[lo] <= pivot:
lo += 1
while arr[hi] > pivot:
hi -= 1
if hi > lo:
arr[lo], arr[hi] = arr[hi], arr[lo]
arr[0], arr[hi] = arr[hi], arr[0]
return hi
def quicksort(l, h):
if l < h:
j = partition(l, h)
quicksort(l, j)
quicksort(j + 1, h)
quicksort(0, 12)
print(arr)
P.S.: I'm a beginner (only 2 months of python) so please try to explain simply. Thanks in advance!
max(arr)+1because it is clearly meant to be larger than any array element.