Tom_Jung

Untitled

Feb 4th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. ##list_a = [3, 7, 8,10]
  2. ##list_b = [2, 4, 5, 9]
  3. ##
  4. def merge(list_a, list_b):
  5. list_c = [ ]
  6. while len(list_a) > 0 and len(list_b) > 0:
  7. if list_a[0] < list_b[0]:
  8. list_c.append(list_a.pop(0))
  9. else:
  10. list_c.append(list_b.pop(0))
  11. if list_a == [ ]:
  12. list_c = list_c + list_b
  13. else:
  14. list_c = list_c + list_a
  15. print("Merged list is ", list_c)
  16. return list_c
  17.  
  18. def merge_sort(unsorted):
  19. if len(unsorted) < 2:
  20. return unsorted
  21. else:
  22. middle = len(unsorted)// 2
  23. front = unsorted[:middle]
  24. back = unsorted[middle:]
  25. print("splits are", front, back)
  26. front = merge_sort(front)
  27. back = merge_sort(back)
  28. return merge(front, back)
  29.  
  30. my_list = [5,4,3,2,1]
  31. merge_sort(my_list)
Advertisement
Add Comment
Please, Sign In to add comment