GIven two different arrays A and B of size N and M, the task is to merge the two arrays which are unsorted into another array which is sorted.
 
Example 1:
Input: N = 3, M = 3
A[] = {10, 5, 15}
B[] = {20, 3, 2}
Output: 2 3 5 10 15 20
Explanation: After merging both the 
array's and sorting it with get the 
desired output.  
Example 2:
Input: N = 4, M = 3
A[] = {1, 10, 5, 15}
B[] = {20, 0, 2}
Output: 0 1 2 5 10 15 20
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function sortedMerge() that takes array A, array B, array res, integer N, and integer M as parameters and stores merged and sorted array in array res.
 
Expected Time Complexity: O (NlogN + MlogM + (N + M))
Expected Auxiliary Space: O(N+M).
 
Constraints:
1 ≤ N, M ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Data structures

Starter code: class Solution:

    def sortedMerge(self, a, b, c, n, m):

        # Your code goes here

Solutions:
class Solution:

	def sortedMerge(self, a, b, c, n, m):
		i = j = k = 0
		while i < n:
			c[k] = a[i]
			k += 1
			i += 1
		while j < m:
			c[k] = b[j]
			k += 1
			j += 1
		c.sort()


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		p = a + b
		p.sort()
		ii = 0
		for i in p:
			c[ii] = i
			ii += 1
		return c


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		a.sort()
		b.sort()
		i = 0
		j = 0
		l = 0
		while i < n and j < m:
			if a[i] <= b[j]:
				c[l] = a[i]
				i += 1
			else:
				c[l] = b[j]
				j += 1
			l += 1
		while i < n:
			c[l] = a[i]
			i += 1
			l += 1
		while j < m:
			c[l] = b[j]
			j += 1
			l += 1


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		a.sort()
		b.sort()
		i = j = k = 0
		while i < len(a) and j < len(b):
			if a[i] < b[j]:
				c[k] = a[i]
				i += 1
			else:
				c[k] = b[j]
				j += 1
			k += 1
		while i < len(a):
			c[k] = a[i]
			k += 1
			i += 1
		while j < len(b):
			c[k] = b[j]
			k += 1
			j += 1
		return c


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		ans = [] + a + b
		ans.sort()
		for i in range(len(c)):
			c[i] = ans[i]
		return


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		l = []
		for i in a:
			l.append(i)
		for i in b:
			l.append(i)
		l.sort()
		for i in range(0, len(l)):
			c[i] = l[i]
		return c


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		for i in range(n):
			c[i] = a[i]
		index = i + 1
		for i in range(m):
			c[index] = b[i]
			index = index + 1
		c.sort()


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		temp = a + b
		temp = sorted(temp)
		for i in range(len(c)):
			c[i] = temp[i]


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		wow = a + b
		wow.sort()
		for i in range(n + m):
			c[i] = wow[i]


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		q = sorted(a + b)
		c.clear()
		for i in range(len(q)):
			c.append(q[i])
		return c


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		a.extend(b)
		a.sort()
		c.clear()
		c.extend(a)


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		a.sort()
		b.sort()
		(i, j) = (0, 0)
		res = []
		while i < n and j < m:
			if a[i] < b[j]:
				res.append(a[i])
				i += 1
			else:
				res.append(b[j])
				j += 1
		while i < n:
			res.append(a[i])
			i += 1
		while j < m:
			res.append(b[j])
			j += 1
		for i in range(len(res)):
			c[i] = res[i]


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		A = []
		for i in range(len(a)):
			A.append(a[i])
		for j in range(len(b)):
			A.append(b[j])
		A.sort()
		for i in range(len(A)):
			c[i] = A[i]


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		c.clear()
		for i in a:
			c.append(i)
		for j in b:
			c.append(j)
		return c.sort()


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		t = a + b
		t = sorted(t)
		k = 0
		for i in t:
			c[k] = i
			k += 1


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		a = a + b
		a.sort()
		c[:] = a[:]


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		a.extend(b)
		i = 0
		for i in range(m + n):
			c[i] = a[i]
		c.sort()


class Solution:

	def sortedMerge(self, a, b, cd, n, m):
		a.extend(b)
		global c
		c = sorted(a)
		return c


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		c[:] = sorted(a + b)


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		d = a + b
		de = sorted(d)
		for i in range(len(d)):
			c[i] = de[i]
		return c


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		res = sorted(a + b)
		for i in range(n + m):
			c[i] = res[i]


class Solution:

	def sortedMerge(self, a, b, c, m, n):
		a.sort()
		b.sort()
		c.clear()
		i = 0
		j = 0
		while i < m and j < n:
			if a[i] <= b[j]:
				c.append(a[i])
				i += 1
			else:
				c.append(b[j])
				j += 1
		while i < m:
			c.append(a[i])
			i += 1
		while j < n:
			c.append(b[j])
			j += 1
		return c


class Solution:

	def sortedMerge(self, a, b, res, n, m):
		a.sort()
		b.sort()
		i = 0
		j = 0
		k = 0
		while i < n and j < m:
			if a[i] < b[j]:
				res[k] = a[i]
				k += 1
				i += 1
			else:
				res[k] = b[j]
				k += 1
				j += 1
		while i < n:
			res[k] = a[i]
			k += 1
			i += 1
		while j < m:
			res[k] = b[j]
			k += 1
			j += 1


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		li = []
		for i in range(n):
			li.append(a[i])
		for i in range(m):
			li.append(b[i])
		li.sort()
		for i in range(n + m):
			c[i] = li[i]
		return c


class Solution:

	def sortedMerge(self, a, b, res, n, m):
		a.sort()
		b.sort()
		total_length = n + m
		i_pointer = 0
		j_pointer = 0
		k_pointer = 0
		while i_pointer < len(a) and j_pointer < len(b):
			if a[i_pointer] < b[j_pointer]:
				res[k_pointer] = a[i_pointer]
				i_pointer += 1
			elif a[i_pointer] == b[j_pointer]:
				res[k_pointer] = a[i_pointer]
				i_pointer += 1
			elif a[i_pointer] > b[j_pointer]:
				res[k_pointer] = b[j_pointer]
				j_pointer += 1
			k_pointer += 1
		while i_pointer < len(a):
			res[k_pointer] = a[i_pointer]
			k_pointer += 1
			i_pointer += 1
		while j_pointer < len(b):
			res[k_pointer] = b[j_pointer]
			k_pointer += 1
			j_pointer += 1


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		for i in range(0, n):
			c[i] = a[i]
		for j in range(0, m):
			c[i + j + 1] = b[j]
		c.sort()
		return c


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		res = a + b
		res.sort()
		for i in range(len(res)):
			c[i] = res[i]


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		d = []
		d = a + b
		d.sort()
		for i in range(n + m):
			c[i] = d[i]
		return c


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		d = a + b
		d.sort()
		return d


class Solution:

	def mergeSort(self, arr):
		if len(arr) > 1:
			mid = len(arr) // 2
			lefthalf = arr[:mid]
			righthalf = arr[mid:]
			self.mergeSort(lefthalf)
			self.mergeSort(righthalf)
			i = j = k = 0
			while i < len(lefthalf) and j < len(righthalf):
				if lefthalf[i] < righthalf[j]:
					arr[k] = lefthalf[i]
					i += 1
				else:
					arr[k] = righthalf[j]
					j += 1
				k += 1
			while i < len(lefthalf):
				arr[k] = lefthalf[i]
				i += 1
				k += 1
			while j < len(righthalf):
				arr[k] = righthalf[j]
				j += 1
				k += 1
		return arr

	def sortedMerge(self, a, b, c, n, m):
		a = self.mergeSort(a)
		b = self.mergeSort(b)
		i = j = k = 0
		while i < len(a) and j < len(b):
			if a[i] < b[j]:
				c[k] = a[i]
				i += 1
			else:
				c[k] = b[j]
				j += 1
			k += 1
		while i < len(a):
			c[k] = a[i]
			k += 1
			i += 1
		while j < len(b):
			c[k] = b[j]
			j += 1
			k += 1
		return c


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		self.a = a
		self.b = b
		self.c = c
		self.a.sort()
		self.b.sort()
		self.len_a = n
		self.len_b = m
		i = j = k = 0
		while i < self.len_a and j < self.len_b:
			if self.a[i] <= self.b[j]:
				self.c[k] = self.a[i]
				i = i + 1
			else:
				self.c[k] = self.b[j]
				j = j + 1
			k = k + 1
		while i < self.len_a:
			self.c[k] = self.a[i]
			i = i + 1
			k = k + 1
		while j < self.len_b:
			self.c[k] = self.b[j]
			j = j + 1
			k = k + 1


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		a.sort()
		b.sort()
		i = 0
		while i != n:
			if a[i] > b[0]:
				val = a[i]
				a[i] = b[0]
				j = 1
				while j != m and b[j] < val:
					b[j - 1] = b[j]
					j += 1
				b[j - 1] = val
			i += 1
		i = 0
		for elem in a:
			c[i] = elem
			i += 1
		for elem in b:
			c[i] = elem
			i += 1


class Solution:

	def sortedMerge(self, a, b, c, n, m):
		i = 0
		for x in sorted(a + b):
			if i <= m + n:
				c[i] = x
			i += 1
		return c