Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # teacher's merge sort
- from random import randint
- list_to_sort = [randint(1,100) for i in range(55)]
- def merge1(list_a, list_b):
- list_c = []
- while len(list_a) > 0 and len(list_b) > 0:
- if list_a[0] < list_b[0]:
- list_c.append(list_a.pop(0))
- else:
- list_c.append(list_b.pop(0))
- if list_a == []:
- list_c += list_b
- else:
- list_c += list_a
- return list_c
- def merge_sort1(unsorted):
- if len(unsorted) < 2:
- return unsorted
- else:
- middle = len(unsorted) // 2
- front = unsorted[:middle]
- back = unsorted[middle:]
- front = merge_sort1(front)
- back = merge_sort1(back)
- return merge1(front, back)
- merge_sort1(list_to_sort)
- print (list_to_sort)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement