merge_sort(num)
return divide(num)
divide(num)
if num is empty or len(num) is 1:
return num
mid = len(num)/2
left = divide(num[:mid])
right = divide(num[mid:])
merge(left,right)
merge(left,right)
merged_list = [ ]
while left and right are not empty:
if left[0] < right[0]:
Pop left[0] and add it to merged_list
else:
Pop right[0] and add it to merged_list
Append remaining left and right to merged_list
Solution key
defmergesort(numbers):# check for the empty list or the list with single elementifnot numbers orlen(numbers)==1:return numbers# recursive splitting (Divide)else: mid =len(numbers)//2 left =mergesort(numbers[:mid]) right =mergesort(numbers[mid:])returnmerge(left, right)defmerge(left,right):# left and right are the sorted lists to be merged# The merged list after sorting merged = []# Loop till left or right becomes emptywhile left and right:# remove the minimum of left[0] and right[0]# and add it to the merged listif left[0]< right[0]: merged += [left.pop(0)]else: merged += [right.pop(0)]# add the remaining elements of left, if any merged += left# add the remaining elements of right, if any merged += rightreturn merged
Pre-Lab Questions
How do you get the number of elements in the list
Merge two lists [2,4,3] and [7,5,6]
Consider left and right lists of size 1. Merge them in a sorted order.
Example:
left = [12] right = [3]
merged = [3,12]
Now consider the two sorted lists of unspecified size. Merge them in a sorted order.
Example:
left = [12,45] right = [3,17]
merged = [3,12,17,45]
4b. Merge two sorted lists [2,6,7] and [3,5,8] in the sorted order
Divide the list num into left and right halves.
Example:
num = [6,2,8,4,3,7,5,1]
left = [6,2,8,4] right = [3,7,5,1]
Recursively divide, till the partition size is 1
Example:
num = [12,3,45,17,15]
left = [12,3]
left = [12]
right = [3]
right = [17, 15]
left = [17]
right = [15]
What are the first two elements to be merged from the input list [6,2,8,4,3,7,5,1] in the mergesort
Validate whether given input is of list type.
Validate whether all the elements in the input are of int type.
Validate whether given list is empty
Post-Lab Questions
Modify the program to validate whether given input is a valid list.
Modify the code to sort the list of string elements.
What is the worst case complexity for merge sort algorithm?
Interview Grade
Why quicksort is better than mergesort?
Is there a way to implement a hybrid sort (which uses MergeSort and InsertionSort)? What is the algorithm for this? Clue: Dynamic Task Parallelism https://msdn.microsoft.com/en-us/library/ff963551.aspx
CyberDojo session
CD extrenal Link : http://cyberdojo1.kgfsl.com/kata/edit/B006BC6DA0?avatar=bee
CD local Link : http://10.100.8.8/kata/edit/B006BC6DA0?avatar=bee
defmergesort(numbers):# Top level function to validate the input onceifnotis_valid(numbers):return"Invalid input"# Original `mergedsort` function is renamed as `split`returnsplit(numbers)defis_valid(numbers):if(type(numbers)!=list):returnFalsefor elem in numbers:ifnotisinstance(elem, int)andnotisinstance(elem, float):returnFalsereturnTrue