Consider left and right lists of size 1. Merge them in a sorted order.
Now consider the two sorted lists of unspecified size. Merge them in a sorted order.
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.
Recursively divide, till the partition size is 1
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.
Bonus
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
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
def mergesort(numbers):
# check for the empty list or the list with single element
if not numbers or len(numbers) == 1:
return numbers
# recursive splitting (Divide)
else:
mid = len(numbers)//2
left = mergesort(numbers[:mid])
right = mergesort(numbers[mid:])
return merge(left, right)
def merge(left, right):
# left and right are the sorted lists to be merged
# The merged list after sorting
merged = []
# Loop till left or right becomes empty
while left and right:
# remove the minimum of left[0] and right[0]
# and add it to the merged list
if 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 += right
return merged
Example:
left = [12] right = [3]
merged = [3,12]
Example:
left = [12,45] right = [3,17]
merged = [3,12,17,45]
Example:
num = [6,2,8,4,3,7,5,1]
left = [6,2,8,4] right = [3,7,5,1]
Example:
num = [12,3,45,17,15]
left = [12,3]
left = [12]
right = [3]
right = [17, 15]
left = [17]
right = [15]
def mergesort(numbers):
# Top level function to validate the input once
if not is_valid(numbers):
return "Invalid input"
# Original `mergedsort` function is renamed as `split`
return split(numbers)
def is_valid(numbers):
if(type(numbers) != list):
return False
for elem in numbers:
if not isinstance(elem, int) and not isinstance(elem, float):
return False
return True