''' 归并排序时间复杂度:O(nlogn) ''' def merge(list1, list2): arr = [] len1 = len(list1) len2 = len(list2) i = 0 j = 0 while i < len1 and j < len2: if list1[i] > list2[j]: arr.append(list2[j]) j += 1 else: arr.append(list1[i]) i += 1 while i < len1: arr.append(list1[i]) i += 1 while j < len2: arr.append(list2[j]) j += 1 return arr def merg_sort(a): len_a = len(a) if len_a <= 1: return a list1 = merg_sort(a[:len_a/2]) list2 = merg_sort(a[len_a/2:]) return merge(list1, list2) a = [5,6,7,1,2,3,4] #测试用例 a = merg_sort(a) print(a)