改进版python实现的归并排序
Posted 做人要厚道2013
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了改进版python实现的归并排序相关的知识,希望对你有一定的参考价值。
def merge(a, begin, mid, end):
left = a[begin:mid]
right = a[mid:end]
left.append(float("inf"))
right.append(float("inf"))
i = 0
j = 0
for k in range(begin, end):
if left[i] < right[j]:
a[k] = left[i]
i = i + 1
else:
a[k] = right[j]
j = j + 1
def merge_sort(a, begin, end):
if begin == end - 1:
return
mid = (begin + end) >> 1
merge_sort(a, begin, mid)
merge_sort(a, mid, end)
merge(a, begin, mid, end)
if __name__ == "__main__":
a = [10, 2, 3, 6, 8, 1, 10, 100, 200, 6] # , 3, 10, 4, 3, 10, 10, 4, 3, 10]
print(a)
merge_sort(a, 0, len(a))
print(a)
以上是关于改进版python实现的归并排序的主要内容,如果未能解决你的问题,请参考以下文章