Python進階-演算法-快速排序

娜娜0130發表於2018-08-23
版權宣告:如需轉載,請註明轉載地址。 https://blog.csdn.net/oJohnny123/article/details/81979571
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
Created by liaoyangyang1 on 2018/8/23 下午1:41.
"""
#快速排序,遞迴演算法 O(nlogn)

list = [4,6,7,82,27,5,4,675,4,67556,565,4]

#遞迴快速排序
def quicksort(list):
    print(`list : {0}`.format(list))
    if len(list)<2:
        return list #基線條件,為空或者只包含一個元素的陣列是有序的
    mid = list[0]#遞迴條件
    lessmid = [i for i in list[1:] if i<=mid]#小於基準值的元素組成的子陣列
    biggermid = [i for i in list[1:] if i > mid]#大於基準值的元素組成的子陣列

    finallylist = quicksort(lessmid)+[mid]+quicksort(biggermid)

    print(`finallylist : {0}`.format(finallylist))
    return finallylist

quicksort(list)

 


相關文章