Q#54: Function to merge and sort arrays

Suppose you’re given two lists, each unsorted. Write a function to take in the two lists, merge them, and then sort them in ascending order. You can call your function MergeSort().

Example:

Input: a = [6, 11, 8], b = [10, 34, 22]
Output: [6, 8, 10, 11, 22, 34]

TRY IT YOURSELF

ANSWER

This question tests our understanding of Python and its Data Structures. On the surface it looks difficult but if you are familiar with Python syntax this is very easy.

First, in python we can combine objects of the same type with the addition symbol, ‘+’. Then, we can use the built in .sort() method for list objects in Python.

def MergeSort(list1, list2):
ans = list1+list2
ans.sort()
return ans

--

--