python-快速排序

def Partitions(list, low, high):
    left = low
    right = high
    base = list[low]
    while left < right:
        while list[left] <= base:
            left = left + 1
        while list[right] > base:
            right = right - 1

        if left < right:
            list[left], list[right] = list[right], list[left]
    list[low] = list[right]
    list[right] = base
    return right


def Quicksort(list, low, high):
    if low < high:
        base = Partitions(list, low, high)
        Quicksort(list, low, base - 1)
        Quicksort(list, base + 1, high)


if __name__ == '__main__':
    mylist = [49, 38, 65, 97, 76, 13, 27, 49]
    Quicksort(mylist, 0, len(mylist) - 1)
    print(mylist)
    print(mylist[len(mylist) - 3])

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章