I agree (or if I am under 13 my parent or guardian agrees on my behalf) to the terms and conditions of use and that: - My test statistics may be published on the site leaderboard against my username - My teacher(s) can review my test scores - I can receive feedback on my tests from my teacher(s)
Mergesort is another comparison-based algorithm also known as a "divide and conquer" algorithm and its main focus is on how to merge together two pre-sorted arrays such that the resulting array is also sorted. You may be interested to know that it was invented by the marvellous John von Neumann in 1945.
The key word of course is 'merge', so we are referring to bringing two lists together. Conceptually, a merge sort works as follows : First, divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted). Then, repeatedly merge sub lists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.
#1 Write a Python program to sort a list of elements using the Merge sort algorithm
#2 Comment each line of the python solution below to show your understanding of the algorithm
Try it yourself
mid = len(nlist)//2
lefthalf = nlist[:mid]
righthalf = nlist[mid:]
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
while i < len(lefthalf):
while j < len(righthalf):
nlist = [14,46,43,27,57,41,45,21,70]