缺点: 消耗空间,less子集和greater子集每次递归就会创建
def quick_sort(array: list):
if len(array) < 2:
return array
else:
pivot = array[0] # 基准值
less = [i for i in array[1:] if i <= pivot] # 小于等于基准值的子集
greater = [i for i in array[1:] if i > pivot] # 大于基准值的子集
return quick_sort(less) + [pivot] + quick_sort(greater)
if __name__ == '__main__':
after_sort = quick_sort([1, 3, 0, 34, 32, 2, 13, 3])
print(after_sort)