标签:
归并排序
1 #!/usr/bin/env python3 2 def mergeSort(array): 3 ‘‘‘归并排序,分解‘‘‘ 4 if len(array) <= 1: 5 return array 6 mid = len(array) // 2 7 left = mergeSort(array[:mid]) 8 right = mergeSort(array[mid:]) 9 return combine(left, right) 10 11 12 def combine(left, right): 13 ‘‘‘合并两个有序list‘‘‘ 14 result = [] 15 while left and right: 16 if left[0] <= right[0]: 17 result.append(left.pop(0)) 18 else: 19 result.append(right.pop(0)) 20 result += left + right 21 return result 22 23 if __name__ == ‘__main__‘: 24 import random 25 L = [] 26 for i in range(30): 27 L.append(random.randint(1, 1000)) 28 print(L) 29 print(mergeSort(L))
标签:
原文地址:http://www.cnblogs.com/aeropig/p/mergeSort.html