How to merge two sorted arrays into a sorted array?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Merging two sorted arrays into one sorted array is a classic linear-time problem. Because the inputs are already ordered, the efficient solution is not to concatenate and sort again, but to walk through both arrays with pointers and always take the smaller next element.
The Standard Two-Pointer Algorithm
The basic idea is simple: keep one index in each array, compare the current values, append the smaller one, and advance the corresponding index.
This runs in O(n + m) time because each element is visited once. The result uses O(n + m) extra space because it creates a new array.
Why This Is Better Than Concatenate Then Sort
It is tempting to write:
That works, but it ignores the fact that the inputs are already sorted. Sorting the combined array costs more work than necessary. The two-pointer approach preserves the precondition and gets the best linear-time result.
In interviews this distinction matters for algorithmic complexity. In real systems it matters for throughput when the arrays are large or the merge happens repeatedly.
C++ Version with Reserved Capacity
The same idea translates directly to C++. Reserving output capacity avoids repeated reallocations.
The logic is identical: compare, append, advance, then copy leftovers.
In-Place Merge When One Array Has Extra Space
Some problems define the first array as having enough trailing capacity to hold the second. In that case, merge from the end so you do not overwrite values you still need to read.
This uses constant extra space and is the right approach for the common "merge sorted array" interview variant.
Edge Cases Worth Testing
Even though the algorithm is small, a few cases reveal most bugs:
- one array is empty
- all values are equal
- negative numbers are present
- one array is much longer than the other
- all values in one array come before the other
Here is a quick check helper:
Testing leftovers matters because many incorrect implementations forget to append the remaining tail once one array is exhausted.
Larger-Scale Merging
The same idea scales beyond interview exercises. Merge sort uses this pattern recursively, and external merge systems use a similar strategy when combining sorted files or streams. The data source changes, but the algorithmic idea stays the same: compare the next available element from each ordered source and take the smaller one.
That continuity is one reason the problem is worth understanding well. It appears in both classroom algorithms and real data-processing systems.
Common Pitfalls
The most common mistake is concatenating and sorting, which throws away the linear-time advantage. Another is forgetting to append leftover elements after the main comparison loop finishes. In in-place variants, developers often try to merge from the front and overwrite unread values. Comparison choice also matters: if you want stable ordering for equal elements, use <= consistently when taking from the left array first.
Summary
- Use two pointers to merge two sorted arrays in linear time.
- Append the smaller current element and advance that pointer.
- Copy the leftover tail after one array runs out.
- Merge from the back for in-place variants with extra buffer space.
- Do not concatenate and sort again unless simplicity matters more than performance.

