快速排序精简Python版
def partition(data, left, right): tmp = data[left] while left < right: while left < right and data[right] >= tmp: right -= 1 data[left] = data[right] while left < right and data[left] <= tmp: left += 1 data[right] = data[left] data[left] = tmp return left def quick_sort(data, left, right): if left < right: mid = partition(data, left, right) quick_sort(data, left, mid-1) quick_sort(data, mid+1, right) print(data) if __name__ == '__main__': li = [3, 4, 2, 6, 1, 7, 9, 5, 8] quick_sort(li, 0, len(li)-1)
结果:
时刻记着自己要成为什么样的人!