-
-
Save k4zy/7820879 to your computer and use it in GitHub Desktop.
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(list): | |
if len(list) <= 1: | |
return list | |
pivot = list[0] | |
left = [] | |
right = [] | |
for val in list[1:len(list)]: | |
if val < pivot: | |
left.append(val) | |
else: | |
right.append(val) | |
left = quicksort(left) | |
right = quicksort(right) | |
return left + [pivot] + right | |
def q_sort(ori_list): | |
if len(ori_list) == 0 : return ori_list | |
pivot = ori_list.pop() | |
min_list = [x for x in ori_list if x <= pivot ] | |
max_list = [x for x in ori_list if x > pivot ] | |
return q_sort(min_list) + [pivot] + q_sort(max_list) | |
if __name__ == "__main__": | |
list = [3, 4, 7, 1, 8, 2, 9, 5] | |
print quicksort(list) | |
print q_sort(list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment