重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

归并排序的python实现

import math

def sort(arr):
    num_count = len(arr)
    n = 0
    while True:
        temp_arr = []
        group_size = 2 ** n

        for i in range(math.ceil(num_count/group_size/2)):
            left = arr[group_size*(2*i):group_size*(2*i+1)]
            right = arr[group_size*(2*i+1):group_size*(2*i+2)]
            while left and right:
                if left[0] > right[0]:
                    temp_arr.append(left.pop(0))
                else:
                    temp_arr.append(right.pop(0))
            temp_arr.extend(left)
            temp_arr.extend(right)
        arr = temp_arr
        if group_size >= num_count:
            break
        n += 1
    return arr

la = [5, 2, 7, 8, 6, 1, 4, 9, 10, 1, 2, 3, 4]
print(sort(la))

当前文章:归并排序的python实现
当前URL:http://cqcxhl.cn/article/jodpie.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP