Last active
May 30, 2018 08:12
-
-
Save vimiix/5cded0e92e9f4e1ab48b9c1ea8b82b62 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
# !/usr/bin/python3 | |
# coding=utf-8 | |
# Time: O(n*logn) | |
# Space: O(n) | |
def qsort(arr): | |
# 基线条件 | |
if len(arr) < 2: | |
return arr | |
else: | |
# 基准值 | |
pivot = arr[0] | |
less = [] | |
greater = [] | |
pivot_list = [] | |
for i in arr: | |
if i < pivot: | |
less.append(i) | |
elif i > pivot: | |
greater.append(i) | |
else: | |
pivot_list.append(i) | |
return qsort(less) + pivot_list + qsort(greater) | |
if __name__ == "__main__": | |
arr = [3,5,1,6,8,4,9] | |
sorted_arr = qsort(arr) | |
print(sorted_arr) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment