gzj137070928 发表于 2020-10-27 17:30:44

归并排序

# 单行输入多个变量的话,用input().split()读入并分解成列表
# 多个整数在一行,则用list(map(int,input().split()))来获取
# merge sort:归并排序

def merge_sort(lst):
    if len(lst) <= 1:
      return lst
    middle = int(len(lst) / 2)
    left = merge_sort(lst[:middle])
    right = merge_sort(lst)
    merged = []
    while left and right:
      merged.append(left.pop(0) if left <= right else right.pop(0))
    merged.extend(right if right else left)
    return merged


data_lst =
print(merge_sort(data_lst))
页: [1]
查看完整版本: 归并排序