Merge Two Sorted Arrays in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include<stdio.h>

int main() {
    int a[100], b[100], c[200], n, m, i=0, j=0, k=0;
    scanf("%d%d", &n, &m);
    for(int x=0; x<n; x++) scanf("%d", &a[x]);
    for(int x=0; x<m; x++) scanf("%d", &b[x]);
    while(i<n && j<m) c[k++] = (a[i]<b[j]) ? a[i++] : b[j++];
    while(i<n) c[k++] = a[i++];
    while(j<m) c[k++] = b[j++];
    for(i=0; i<k; i++) printf("%d ", c[i]);
}

C Output

Input:  
3 3  
1 3 5  
2 4 6  

Output:  
1 2 3 4 5 6


C++ Program

#include<iostream>
using namespace std;

int main() {
    int a[100], b[100], c[200], n, m, i=0, j=0, k=0;
    cin >> n >> m;
    for(int x=0; x<n; x++) cin >> a[x];
    for(int x=0; x<m; x++) cin >> b[x];
    while(i<n && j<m) c[k++] = (a[i]<b[j]) ? a[i++] : b[j++];
    while(i<n) c[k++] = a[i++];
    while(j<m) c[k++] = b[j++];
    for(i=0; i<k; i++) cout << c[i] << " ";
}

C++ Output

Input:  
4 2  
1 4 7 9  
2 5  

Output:  
1 2 4 5 7 9


JAVA Program

import java.util.*;

class MergeSorted {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt();
        int a[] = new int[n], b[] = new int[m], c[] = new int[n+m], i=0, j=0, k=0;
        for(int x=0; x<n; x++) a[x] = sc.nextInt();
        for(int x=0; x<m; x++) b[x] = sc.nextInt();
        while(i<n && j<m) c[k++] = (a[i]<b[j]) ? a[i++] : b[j++];
        while(i<n) c[k++] = a[i++];
        while(j<m) c[k++] = b[j++];
        for(i=0; i<k; i++) System.out.print(c[i] + " ");
    }
}

JAVA Output

Input:  
2 4  
2 9  
1 3 5 7  

Output:  
1 2 3 5 7 9


Python Program

a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = j = 0; c = []
while i < len(a) and j < len(b):
    c.append(a[i]) if a[i] < b[j] else c.append(b[j]); i += a[i] < b[j]; j += a[i] >= b[j]
c += a[i:] + b[j:]
print(*c)

Python Output

Input:  
[1, 2, 5]  
[3, 4, 6]  

Output:  
1 2 3 4 5 6


In-Depth Learning – Entire Concept in Paragraphs
What Is Merging Two Sorted Arrays?
Merging two sorted arrays is the process of merging two already sorted lists into one single sorted list. It is a fundamental operation in numerous sorting algorithms, especially in Merge Sort and in data merge operations such as log processing, external sorting, and database joining.

How the Code Works
We keep three pointers:

i for array a

j for array b

k for merged array c

We:

Compare entries at a[i] and b[j]

Place the smaller in c[k]

Advance the pointer (whatever element was smaller)

Do repeatedly until one array is exhausted

Copy the rest of the other array

This leaves the resultant merged array c[] sorted.

Example
Input:


a = [2, 4, 6]
b = [1, 3, 5]
Steps:

Compare 2 & 1 → take 1

Compare 2 & 3 → take 2

Compare 4 & 3 → take 3

Compare 4 & 5 → take 4

Compare 6 & 5 → take 5

Only 6 is left → take 6

Output:

[1, 2, 3, 4, 5, 6]

Real-Life Analogy
Picture two lines of sorted individuals waiting on ticketing at two tills. You need to create one orderly line merging the two without causing a disarray. So you pick one individual from each line in sorted age order and create a new merged queue — this is what the merge function accomplishes!

It's also like merging two sorted decks of cards into one without mixing them up.

Where and When Is It Used?
This method is widely employed in:

Merge Sort algorithm

External sorting (when data is larger than memory)

Merging logs or data streams

Databases and search engines

System logs or version control diffs

It is an important operation in big systems where merging and sorting go hand in hand.

Time and Space Complexity
Operation\tComplexity
Time\tO(n + m)
Space\tO(n + m)

All elements are handled once and stored in the output array.

In some optimized scenarios (like merging in-place), space can be reduced — but it increases complexity.

Python-Specific Advantage
Python allows elegant one-liners with libraries:

python

import heapq
print(*heapq.merge(a, b))
This merges two sorted iterables efficiently.

SEO-Optimized Natural Paragraph for Ranking
Looking to merge two sorted arrays in C, C++, Java, or Python? This tutorial demonstrates how to efficiently combine two pre-sorted arrays into one sorted array. This algorithm forms the basis of Merge Sort and is extremely common in competitive programming, system logs, and real-time data processing. This solution provides optimal space usage and minimum time complexity for real-world merge operations. Understand how to merge sorted arrays with simple loops, pointers, and elegant logic using complete working code and real-life examples for better comprehension.