Skip to content

Instantly share code, notes, and snippets.

@yuckiymouse
Created September 23, 2019 10:31
Show Gist options
  • Save yuckiymouse/dc8bf99fe7af2411fd85f2bee36a90c9 to your computer and use it in GitHub Desktop.
Save yuckiymouse/dc8bf99fe7af2411fd85f2bee36a90c9 to your computer and use it in GitHub Desktop.
quick sort
def quick_sort(arr):
# make 2 groups
left = []
right = []
if len(arr) <= 1:
return arr
# pick random number as pivot
ref = random.choice(arr)
for ele in arr:
if ele < ref:
left.append(ele)
elif ele > ref:
right.append(ele)
else:
ref_count += 1
left = quick_sort(left)
right = quick_sort(right)
return left + [ref] * ref_count + right
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment