def quicksort(nums):
import random
if len(nums) <= 1:
return nums
else:
q = random.choice(nums)
l_nums = [n for n in nums if n < q]
e_nums = [q] * nums.count(q)
b_nums = [n for n in nums if n > q]
return quicksort(l_nums) + e_nums + quicksort(b_nums)
print(quicksort([12, 8, 6, 13, 56, 78, 3, 46]))
This is a recursive solution, it works faster
Comments
Leave a comment