1.快速排序介绍
1.从序列中任意挑出一个元素,作为基准
2.重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面
3.递归地把小于基准值元素的子数列和大于基准值元素的子数列排序
2. 代码实现
def quick_sort(lst, start, end):
if start < end:
i, j = start, end
base = lst[i]
while i < j:
while (i < j) and (lst[j] >= base):
j = j - 1
lst[i] = lst[j]
while (i < j) and (lst[i] <= base):
i = i + 1
lst[j] = lst[i]
lst[i] = base
quick_sort(lst, start, i - 1)
quick_sort(lst, j + 1, end)
return lst
lst = [7,6,5,3,12,4,15,1]
print(lst)
print('-'*30)
print(quick_sort(lst,0,len(lst) - 1))
# 运行结果
[7, 6, 5, 3, 12, 4, 15, 1]
--------------------------
[1, 3, 4, 5, 6, 7, 12, 15]