I have written an assignment for a data structures class and am having trouble figuring out how to "Implement the main quicksort function that recursively sorts the subarrays formed by partitioning" which implies the recursion happens in the main quicksort function, while also " [having] a method signature of: quicksort(arr) — Sorts the list arr in-place and returns nothing"
I am struggling to see how I can implement it to the word of the assignment without doing silly things. The TA is a really really tough marker and needs everything to be to the letter of the assignment (even things not explicitly stated anywhere in the assignment or slides). All I am requesting is if what I am being asked is even technically possible.
Right now, I have the following:
def partition(arr, low, high):
pivot = arr[high]
pivIndex = high
while True:
while arr[low] <= pivot and low != high:
low += 1
while arr[high] >= pivot and low != high:
high -= 1
if low == high:
arr[low], arr[pivIndex] = arr[pivIndex], arr[low]
return low
else:
arr[low], arr[high] = arr[high], arr[low]
def quicksort(arr, low, high):
#the subarray has strictly less than two elements
if high <= low:
return
pivot = round(random.random() * (high - low) + low)
arr[pivot], arr[high] = arr[high], arr[pivot]
pivot = partition(arr, low, high)
quicksort(arr, pivot + 1, high)
quicksort(arr, low, pivot - 1)
return arr
list.sort()method which sorts the list in place and returnsNone. That is what your quicksort algorithm should do. Modify the array in place, but it doesn't need to return any values.sorted()) or they modify the existing object in place (likelist.sort()). In the latter case they don't return the object that was updated.