python归并排序

由于某人问我要个归并排序,就凑合写了一个,
AA = raw_input().split(' ') A = []; for num in AA: A.append(int(num)) def MergeSort(A): if len(A) <= 1: return A else: mid = int(len(A)/2) left = MergeSort(A[:mid]) right = MergeSort(A[mid:]) return Merge(left,right) def Merge(left,right): r, l =0, 0 result = [] while r < len(right) and l < len(left): if left[l] < right[r]: result.append(left[l]) l += 1 else: result.append(right[r]) r += 1 result += left[l:] result += right[r:] return result print MergeSort(A)

 

posted on 2016-06-12 21:24  Kermit.Li  阅读(135)  评论(0编辑  收藏  举报

导航