Skip to content

Instantly share code, notes, and snippets.

@cgopalan
Created April 30, 2021 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgopalan/710f532cedfa67f5f11daca91afc3080 to your computer and use it in GitHub Desktop.
Save cgopalan/710f532cedfa67f5f11daca91afc3080 to your computer and use it in GitHub Desktop.
Quicksort
from typing import List
def quicksort(ints: List[int]):
if len(ints) < 2 or len(set(ints)) == 1:
return ints
pivot = ints[0]
ints_less_than_pivot = [x for x in ints[1:] if x <= pivot]
ints_more_than_pivot = [x for x in ints[1:] if x > pivot]
return quicksort(ints_less_than_pivot) + [pivot] + quicksort(ints_more_than_pivot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment