1. 程式人生 > >python排序之歸併排序

python排序之歸併排序

思想:先遞分解陣列,再並陣列

程式碼:

def merge_sort(alist):
    n=len(alist)
    if n<=1:
        return alist
    mid=n//2
    left_li=merge_sort(alist[:mid])
    right_li=merge_sort(alist[mid:])
    left_pointer,right_pointer=0,0
    result=[]
    while left_pointer<len(left_li) and right_pointer<len(right_li):
        if left_li[left_pointer]<=right_li[right_pointer]:
            result.append(left_li[left_pointer])
            left_pointer+=1
        else:
            result.append(right_li[right_pointer])
            right_pointer+=1
    result+=left_li[left_pointer:]
    result+=right_li[right_pointer:]
    return result

最優時間複雜度:O(nlogn)

最差時間複雜度:O(nlogn)

穩定性:穩定