mergesort_arithmetic_python

Posted Edge of Eternity

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mergesort_arithmetic_python相关的知识,希望对你有一定的参考价值。

 1 def merge(a, b):
 2     c = []
 3     h = j = 0
 4     while j < len(a) and h < len(b):
 5         if a[j] < b[h]:
 6             c.append(a[j])
 7             j += 1
 8         else:
 9             c.append(b[h])
10             h += 1
11 
12     if j == len(a):
13         for i in b[h:]:
14             c.append(i)
15     else:
16         for i in a[j:]:
17             c.append(i)
18 
19     return c
20 
21 
22 def merge_sort(lists):
23     if len(lists) <= 1:
24         return lists
25     middle = int(len(lists)/2)
26     left = merge_sort(lists[:middle])
27     right = merge_sort(lists[middle:])
28     return merge(left, right)
29 
30 
31 if __name__ == __main__:
32     a = [4, 7, 8, 3, 5, 9]
33     print(merge_sort(a))

 

以上是关于mergesort_arithmetic_python的主要内容,如果未能解决你的问题,请参考以下文章