I have this code but the employer has asked me to pivot in the middle If anyone can help, please edit this code
def quicksort(sequence, low, high):
if low < high:
pivot = partition(sequence, low, high)
quicksort(sequence, low, pivot - 1)
quicksort(sequence, pivot + 1, high)
def partition(sequence, low, high):
pivot = sequence[low]
i = low + 1
for j in range(low + 1, high + 1):
if sequence[j] < pivot:
sequence[j], sequence[i] = sequence[i], sequence[j]
i += 1
sequence[i-1], sequence[low] = sequence[low], sequence[i-1]
return i - 1
patel's answer switches from the question's Lomuto scheme to Hoare scheme. The simplest fix to the question's code would be swapping middle element with low element as the first line in partition
def partition(sequence, low, high):
sequence[low],sequence[(low+high)//2] = sequence[(low+high)//2],sequence[low] #change
pivot = sequence[low]
...