D.数据结构(快速排序)

A.快速排序

1.算法逻辑

若要对

def quick_sort(alist,first,last):
    if first >= last:
        # 递归结束条件
        return
    high = last
    low = first
    media_data = alist[first]
    while low < high:
        while low < high and alist[high] > media_data:
            # 必须先从high开始
            high -= 1
        alist[low] = alist[high]

        while low < high and alist[low] < media_data:
            low += 1
        alist[high] = alist[low]

    alist[low] = media_data

    quick_sort(alist, first, low-1)
    quick_sort(alist, low+1, last)


if __name__ == "__main__":
    li = [24, 12, 56, 34, 78, 25, 94, 77]
    quick_sort(li, 0, len(li)-1)
    print(li)

 

上一篇:python记录汇总


下一篇:列表去重