其他分享
首页 > 其他分享> > 快速排序

快速排序

作者:互联网

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[right] = tmp
    return left


def quick_sort(data, left, right):
    if left < right:
        mid = partition(data, left, right)
        print(mid)
        quick_sort(data, left, mid - 1)
        quick_sort(data, mid + 1, right)


l1 = [5, 6, 7, 3, 4, 8, 2, 9]

quick_sort(l1, 0, len(l1) - 1)
print(l1)

 

标签:tmp,right,快速,partition,while,排序,data,left
来源: https://www.cnblogs.com/maplethefox/p/16673230.html