def quick_sort(alist, start, end): if start >= end: # 退出递归 return pivot = alist[start] right = end left = start # 控制right -= 1不满足条件交换 while left < right: while left < right and alist[right] > pivot: right -= 1 else: # 交换 alist[left] = alist[right] # 控制 left += 1 , 不满足条件交换 while left < right and alist[left] < pivot: left += 1 else: alist[right] = alist[left] # 退出循环 left = right # left 或者 right 对应的位置 赋值为基准值 alist[left] = pivot # 递归自己调用自己 quick_sort(alist, start, left-1) # 对左边排序 quick_sort(alist, left+1, end) # 对右边排序 if __name__ == '__main__': li = [6, 7, 5, 3, 4, 1, 8] quick_sort(li, 0, len(li)-1) print(li)