Quick sort algo
from random import randrange
input = [10, 5, 2, 3, 7, 0, 9, 12]
def quicksort(arr):
if len(arr) < 2:
return arr
else:
rand = randrange(0, len(arr)) # grab a random index
pivot = arr.pop(rand)
less = [i for i in arr if i <= pivot]
greater = [i for i in arr if i > pivot]
return quicksort(less) + [pivot] + quicksort(greater)
print("sorted: ", quicksort(input))
February 6, 2022 at 4:05:26 PM GMT+1
- permalink
-
-
https://www.integralist.co.uk/posts/algorithmic-complexity-in-python/