快速排序,在等待排序的n个值中选取一个值做为中间值,将大于中间值的放右边,小于中间值的放左边,然后再分别对左右子表进行上面重复操作,直到子表只有一个值为止。
代码实现
# -*- coding = utf-8 -*- # @Author: Wchime # @time: 2023/1/27 17:04 # @file: 快速排序.py def sort_quick(li,left,right): """快速排序""" if left >= right: return mid_value = li[left] low = left high = right while low < high: while low < high and li[high] >= mid_value: high -= 1 li[low] = li[high] while low < high and li[low] < mid_value: low += 1 li[high] = li[low] li[low] = mid_value sort_quick(li, left, low-1) sort_quick(li, low+1, right) if __name__ == "__main__": l = [1, 9, 4, 8, 2, 3, 7] sort_quick(l, 0, len(l)-1) print(l)
标签:__,li,high,low,排序,快速,left From: https://www.cnblogs.com/moon3496694/p/17070843.html