from statistics import median
def q_sort(array):
print(array)
if len(array)<=1:#return array when the length is less than and equal to 1, as sublist will have 0 and 1 length at the end
return array
else:
# best way to take a pivot is by median of first ,last and the middle element
pivot = median([array[0],array[int((len(array)-1)/2)],array[len(array)-1]])
array.remove(pivot)#removing the pivot from the list
list_r,list_l=[],[]# creating left and right list
for i in array:
if (i<=pivot):
list_l.append(i)
else:
list_r.append(i)
print(list_l,list_r,pivot)
return q_sort(list_l) + [pivot] +q_sort(list_r)
I think mine is easier
from statistics import median
def q_sort(array):
print(array)
if len(array)<=1:#return array when the length is less than and equal to 1, as sublist will have 0 and 1 length at the end
q_sort([9,5,1,-5,10,0,23])