Skip to content

Instantly share code, notes, and snippets.

@femmerling
Created December 23, 2012 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save femmerling/4364026 to your computer and use it in GitHub Desktop.
Save femmerling/4364026 to your computer and use it in GitHub Desktop.
The quick sort Algorithm written in python.
def quicksort(input_list):
higher = []
lower = []
if len(input_list) > 2:
pivot = (len(input_list) - 1)/2
mid = [input_list[pivot]]
i = 0
while i < len(input_list):
if i != pivot:
if input_list[i] <= input_list[pivot]:
lower.append(input_list[i])
elif input_list[i] > input_list[pivot]:
higher.append(input_list[i])
i=i+1
return quicksort(lower)+mid+quicksort(higher)
elif len(input_list) == 2:
if input_list[0] > input_list[1]:
input_list[0],input_list[1] = input_list[1],input_list[0]
return input_list
else:
return input_list
#test run
number_list = [3,1,5,3,2,5,8,2,9,6,12,53,75,22,83,123,12123]
print quicksort(number_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment