Быстрая сортировка со случайным опорным элементом
В таком виде сортирует нормально.
def quicksort(array):
if len(array) < 2:
return array
else:
pivot = array[0]
less = [i for i in array[1:] if i <= pivot]
greater = [i for i in array[1:] if i > pivot]
return quicksort(less) + [pivot] + quicksort(greater)
print(quicksort([100, 2, 10, 8, 1, 90]))
А как сделать, что бы выбирал случайны опорный элемент? ибо вот так результат не соответствует желаемому
import random
def quicksort(array):
if len(array) < 2:
return array
else:
pivot = random.choice(array)
less = [i for i in array[1:] if i <= pivot]
greater = [i for i in array[1:] if i > pivot]
return quicksort(less) + [pivot] + quicksort(greater)
print(quicksort([100, 2, 10, 8, 1, 90]))