快排

 

def partition(li, left, right):
    tmp = li[left]
    while left < right:
        # 从右边找比tmp小的数
        while left < right and li[right] >= tmp:
            right -= 1
        li[left] = li[right]
        # 从左边找比tmp大的数
        while left < right and li[left] <= tmp:
            left +=1
        li[right] = li[right]
    li[left] = tmp
    li[left] = tmp

def quick_sort(li, left, right):
    if left < right:   #至少两个元素
        mid = partition(li, left, right)
        quick_sort(li, left, mid - 1)
        quick_sort(li, mid + 1, right)

# def _quick_sort(li, left, right):
#     if left < right:    # 至少有两个元素
#         mid = partition(li, left, right)
#         _quick_sort(li, left, mid-1)
#         _quick_sort(li, mid+1, right)

 

posted @ 2019-10-16 13:54  虫洞小鳗鱼  阅读(119)  评论(0编辑  收藏  举报