您的位置:首页 > 编程语言 > Python开发

mergesort in python

2014-05-04 21:47 267 查看
def mergesort(List):
""" input : List, an integer list
output : an sorted integer list
"""
n = len(List)
if n == 0 or n==1:
return List
first = mergesort(List[:n/2])
second =  mergesort(List[n/2:])
i=0
j=0
output = []
while True:
if first[i]<second[j]:
output.append(first[i])
i += 1
else:
output.append(second[j])
j += 1
if i >= len(first):
output += second[j:]

break
if j >= len(second):
output += first[i:]

break

return output
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: