Created
December 23, 2012 15:53
-
-
Save femmerling/4364026 to your computer and use it in GitHub Desktop.
The quick sort Algorithm written in python.
This file contains 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(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