Last active
January 15, 2021 16:21
-
-
Save kozmonaut/e73315c006f9f685032e to your computer and use it in GitHub Desktop.
Quicksort in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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