标签:ret ide bsp 部分 pen quic closed partition 条件
快排:
data = [2, 5, 1, 6, 3, 9, 7] def quick_sort(data, left, right): if left < right: # 1 确定中间数的位置, 求出中间数的索引 mid = partition(data, left, right) print(mid) # 2 对剩余的两部分排序, 采用递归 quick_sort(data, left, mid - 1) quick_sort(data, mid + 1, right) print(data) def partition(data, left, right): tmp = data[left] while left < right: # 加left < right的原因: 由于跳不出循环, 所以加上条件, 要么是left +1 , 或者是right - 1. while tmp < data[right] and left < right: right = right - 1 data[left] = data[right] while tmp > data[left] and left < right: left = left + 1 data[right] = data[left] data[left] = tmp return left quick_sort(data, 0, len(data) - 1)
标签:ret ide bsp 部分 pen quic closed partition 条件
原文地址:https://www.cnblogs.com/gyh412724/p/10140209.html