Skip to content

Instantly share code, notes, and snippets.

@kozmonaut
Last active January 15, 2021 16:21
Show Gist options
  • Save kozmonaut/e73315c006f9f685032e to your computer and use it in GitHub Desktop.
Save kozmonaut/e73315c006f9f685032e to your computer and use it in GitHub Desktop.
Quicksort in Python
def quicksort(list):
# Define lists
less = []
equal = []
greater = []
if len(list) > 1: # Check if list have more than 1 element
pivot = list[0] # This is now pivot value
for number in list: # Go through numbers in list
# Compare numbers with pivot value and add it to a right list
if number < pivot:
less.append(number)
elif number == pivot:
equal.append(number)
elif number > pivot:
greater.append(number)
# Sort and connect lists
return sort(less) + equal + sort(greater)
else:
# If list have only one value return list untouched
return list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment