马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# 单行输入多个变量的话,用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[middle:])
merged = []
while left and right:
merged.append(left.pop(0) if left[0] <= right[0] else right.pop(0))
merged.extend(right if right else left)
return merged
data_lst = [6, 202, 100, 301, 38, 8, 1]
print(merge_sort(data_lst))
|