Advertisement
zhongnaomi

teacher's merge sort

Feb 14th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. # teacher's merge sort
  2. from random import randint
  3.  
  4. list_to_sort = [randint(1,100) for i in range(55)]
  5. def merge1(list_a, list_b):
  6.     list_c = []
  7.     while len(list_a) > 0  and len(list_b) > 0:
  8.         if list_a[0] < list_b[0]:
  9.             list_c.append(list_a.pop(0))
  10.         else:
  11.             list_c.append(list_b.pop(0))
  12.     if list_a == []:
  13.         list_c += list_b
  14.     else:
  15.         list_c += list_a
  16.    
  17.     return list_c
  18.  
  19.  
  20. def merge_sort1(unsorted):
  21.     if len(unsorted) < 2:
  22.         return unsorted
  23.     else:
  24.         middle = len(unsorted) // 2
  25.         front = unsorted[:middle]
  26.         back = unsorted[middle:]
  27.        
  28.         front = merge_sort1(front)
  29.         back = merge_sort1(back)
  30.     return merge1(front, back)
  31.  
  32. merge_sort1(list_to_sort)
  33. print (list_to_sort)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement