def quick_sort_standard(nums, l, r):
if l < r:
mid = partion(nums, l, r)
quick_sort_standard(nums, l, mid)
quick_sort_standard(nums, mid+1, r)
def partion(nums, l, r):
# 每次partition后都能确定pivot位置返回pivot的index
pivot = nums[l] # 将nums[l]作为中间的标杆
while l < r:
while l < r and nums[r] >= pivot:
r -= 1
nums[l] = nums[r]
while l < r and nums[l] <= pivot:
l += 1
nums[r] = nums[l]
nums[l] = pivot
return l # 最后l == r
if __name__ == '__main__':
nums = [9, 3, 2, 1, 4, 6, 7, 0, 5]
print(nums)
quick_sort_standard(nums, 0, len(nums)-1)
print(nums)
快速排序 python实现
最新推荐文章于 2021-07-02 09:57:54 发布