Merge Two Sorted Arrays solved by 2181

Feb. 21, 2014, 4:22 p.m. by Rosalind Team

Topics: Sorting

Problem

The merging procedure is an essential part of “Merge Sort” (which is considered in one of the next problems).

Given: A positive integer n105 and a sorted array A[1..n] of integers from 105 to 105, a positive integer m105 and a sorted array B[1..m] of integers from 105 to 105.

Return: A sorted array C[1..n+m] containing all the elements of A and B.

Sample Dataset

4
2 4 10 18
3
-5 11 12

Sample Output

-5 2 4 10 11 12 18

Hintclick to expand

The very first element of C is either A[1] or B[1], whichever is smaller. The rest of C can then be constructed recursively.

Source: Algorithms by Dasgupta, Papadimitriou, Vazirani. McGraw-Hill. 2006.

Please login to solve this problem.