merge sort in Python

merge sort

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

    result += left_li[left_p:]
    result += right_li[right_p:]
    return result

def main():
    list_01 = [2, 4, 55, 1, 6, 28, 54, 55, 19, 95, 48, 65]
    merge_sort(list_01)
    print(list_01)
    print(merge_sort(list_01))

if __name__ == '__main__':
    main()

---------------results of enforcement----------
[1, 2, 4, 6, 19, 28, 48, 54, 55, 55, 65, 95]

-----------------------------------------------
展开阅读全文

没有更多推荐了,返回首页