Given 2 arrays with int, A1 and A2. A1 length is L1, A2 length is L1+L2.
The first L2 elements of A1 and A2 have been sorted. Combine the first L2 elements into A2.
e.g.
A1: 1 3 6 0
A2: 2 5 7 13 10 22 11
result:
A1 + A2: 1 2 3 5 6 7 13 10 22 11
my solutions:
Pick each element by putting min { A1[i], A2[i]} into a array B[i], O(2 * L2)
Copy B to A2. O(L1 + L2).
Unsorted part should not be changed.
Any better solutions ?
thanks
I chose to show A1 and A1 as static arrays, but if you’re getting them as
int*s to heap-allocated arrays, and if it’s important that the finished array be placed in L2, you can then saydelete[] L2; L2 = out;after the call tosemiMerge(). I chose not to do this in main because I representedA2as a static array, while switching it out for the contents ofoutwould require it to be a pointer to a dynamic array.