Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Wed Feb 13 19:48:09 2019
- @author: Naomi Toshiba
- """
- from random import randint
- list_to_sort = [randint(1,100) for i in range(55)]
- print(list_to_sort)
- def merge(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))
- while len(list_a) > 0:
- list_c.append(list_a.pop(0))
- while len(list_b) > 0:
- list_c.append(list_b.pop(0))
- print("merged list is", list_c)
- return list_c
- def merge_sort(unsorted):
- if len(unsorted) <=1:
- return unsorted
- front =[]
- back =[]
- for i,x in enumerate(unsorted,start=0):
- if i < len(unsorted)/2:
- front.append(x)
- else:
- back.append(x)
- print("splits are", front, back)
- front = merge_sort(front)
- back = merge_sort(back)
- return merge(front, back)
- merge_sort(list_to_sort)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement