#!python
#coding=utf-8
"""
快速排序
"""
def quick_sort(arr):
if len(arr) < 2:
return arr
# 基准值
pivot = arr[0]
# 过滤出比基准值小的值组成子数组
less = [i for i in arr[1:] if i < pivot]
# 过滤出比基准值大的值组成子数组
greater = [i for i in arr[1:] if i > pivot]
return quick_sort(less) + [pivot] + quick_sort(greater)
print(quick_sort([52, 5, 2, 56, 80, 54, 99]))
运行结果
[2, 5, 52, 54, 56, 80, 99]