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))