Guest User

Untitled

a guest
Jan 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. def merge_sort(list_sort):
  2. """splits the list in two parts until each part is left with one member"""
  3.  
  4. if len(list_sort) == 1:
  5. return list_sort
  6.  
  7. if len(list_sort)>= 2:
  8. x= len(list_sort) / 2
  9. part_a = list_sort[:x]
  10. part_b = list_sort[x:]
  11. sorted_part_a = merge_sort(part_a)
  12. sorted_part_b = merge_sort(part_b)
  13. return merge(sorted_part_a, sorted_part_b)
  14.  
  15. def merge(left , right):
  16. """merges the two parts of list after sorting them"""
  17. sorted_list = []
  18. i = 0
  19. while left[:] and right[:] :
  20. if left [i] > right [i]:
  21. sorted_list.append(right[i])
  22. right.remove(right[i])
  23.  
  24. else :
  25. sorted_list.append(left[i])
  26. left.remove(left[i])
  27.  
  28. if left[:]:
  29. sorted_list.extend(left[:])
  30. elif right[:] :
  31. sorted_list.extend(right[:])
  32. return sorted_list
  33.  
  34. details =
  35. [1, 127, 56, 2, 1, 5, 7, 9, 11, 65, 12, 24, 76, 87, 123, 65, 8,32, 86, 123, 67, 1, 67, 92, 72, 39, 49, 12, 98, 52, 45, 19, 37, 22, 1, 66, 943, 415, 21, 785, 12, 698, 26, 36, 18, 97, 0, 63, 25, 85, 24, 94, 150 ]
  36. print "List to be sorted = ", details
  37. print "Sorted List = ", merge_sort(details)
Add Comment
Please, Sign In to add comment