Merge Two Arrays: flowchart, algorithm and program
This program reads two arrays, a and b, and builds a third array c that holds all elements of a followed by all elements of b. It uses one counter, k, that keeps track of the next free position in c while both arrays are copied in.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Read n1.
- Step 3: Set i = 0.
- Step 4: If i < n1, go to step 5; otherwise go to step 6.
- Step 5: Read a[i]; i++. Go to step 4.
- Step 6: Read n2.
- Step 7: Set i = 0.
- Step 8: If i < n2, go to step 9; otherwise go to step 10.
- Step 9: Read b[i]; i++. Go to step 8.
- Step 10: Set k = 0, i = 0.
- Step 11: If i < n1, go to step 12; otherwise go to step 13.
- Step 12: Set c[k++] = a[i]; i++. Go to step 11.
- Step 13: Set i = 0.
- Step 14: If i < n2, go to step 15; otherwise go to step 16.
- Step 15: Set c[k++] = b[i]; i++. Go to step 14.
- Step 16: Set i = 0.
- Step 17: If i < k, go to step 18; otherwise go to step 19.
- Step 18: Print c[i]; i++. Go to step 17.
- Step 19: Stop.
Pseudocode
BEGIN
READ n1
i ← 0
WHILE i < n1 DO
READ a[i]; i++
END WHILE
READ n2
i ← 0
WHILE i < n2 DO
READ b[i]; i++
END WHILE
k ← 0, i ← 0
WHILE i < n1 DO
c[k++] ← a[i]; i++
END WHILE
i ← 0
WHILE i < n2 DO
c[k++] ← b[i]; i++
END WHILE
i ← 0
WHILE i < k DO
PRINT c[i]; i++
END WHILE
END
Program in C, Python and Java
Merge Two Arrays program in C
#include <stdio.h>
int main() {
int a[50], b[50], c[100];
int n1, n2, i, k = 0;
printf("Enter size of first array: ");
scanf("%d", &n1);
printf("Enter elements: ");
for (i = 0; i < n1; i++) {
scanf("%d", &a[i]);
}
printf("Enter size of second array: ");
scanf("%d", &n2);
printf("Enter elements: ");
for (i = 0; i < n2; i++) {
scanf("%d", &b[i]);
}
for (i = 0; i < n1; i++) {
c[k++] = a[i];
}
for (i = 0; i < n2; i++) {
c[k++] = b[i];
}
printf("Merged array: ");
for (i = 0; i < k; i++) {
printf("%d ", c[i]);
}
printf("\n");
return 0;
}Merge Two Arrays program in Python
n1 = int(input("Enter size of first array: "))
a = list(map(int, input("Enter elements: ").split()))
n2 = int(input("Enter size of second array: "))
b = list(map(int, input("Enter elements: ").split()))
c = []
for i in range(n1):
c.append(a[i])
for i in range(n2):
c.append(b[i])
print("Merged array:", *c)Merge Two Arrays program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of first array: ");
int n1 = sc.nextInt();
int[] a = new int[n1];
System.out.print("Enter elements: ");
for (int i = 0; i < n1; i++) {
a[i] = sc.nextInt();
}
System.out.print("Enter size of second array: ");
int n2 = sc.nextInt();
int[] b = new int[n2];
System.out.print("Enter elements: ");
for (int i = 0; i < n2; i++) {
b[i] = sc.nextInt();
}
int[] c = new int[n1 + n2];
int k = 0;
for (int i = 0; i < n1; i++) {
c[k++] = a[i];
}
for (int i = 0; i < n2; i++) {
c[k++] = b[i];
}
System.out.print("Merged array: ");
for (int i = 0; i < k; i++) {
System.out.print(c[i] + " ");
}
System.out.println();
}
}
Sample input: n1 = 3, a = 1 2 3, n2 = 2, b = 4 5
Output: Merged array: 1 2 3 4 5
How it works
- Read n1 and the n1 elements of array a, then read n2 and the n2 elements of array b.
- Set k = 0. This is the next empty slot in the merged array c.
- The first loop runs i from 0 to n1 - 1 and does c[k++] = a[i], so every element of a is copied into c and k moves forward each time.
- The second loop runs i from 0 to n2 - 1 and does c[k++] = b[i]. Because k was not reset, b's elements land right after a's.
- After both loops, k equals n1 + n2, which is the size of c. The last loop prints c[0] to c[k - 1].
Dry run (trace table)
| Step | Line | Box | n1 | i | a | n2 | b | k | c | Output |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 3 | Start | ||||||||
| 2 | 8 | Read n1 | 3 | Enter size of first array: Enter elements: | ||||||
| 3 | 10 | i = 0 | 3 | 0 | ||||||
| 4 | 10 | i < n1? | 3 | 0 | ||||||
| 5 | 11 | Read a[i]; i++ | 3 | 1 | [1,_,_] | |||||
| 6 | 10 | i < n1? | 3 | 1 | [1,_,_] | |||||
| 7 | 11 | Read a[i]; i++ | 3 | 2 | [1,2,_] | |||||
| 8 | 10 | i < n1? | 3 | 2 | [1,2,_] | |||||
| 9 | 11 | Read a[i]; i++ | 3 | 3 | [1,2,3] | |||||
| 10 | 10 | i < n1? | 3 | 3 | [1,2,3] | |||||
| 11 | 15 | Read n2 | 3 | 3 | 2 | Enter size of second array: Enter elements: | ||||
| 12 | 17 | i = 0 | 0 | 2 | ||||||
| 13 | 17 | i < n2? | 0 | 2 | ||||||
| 14 | 18 | Read b[i]; i++ | 1 | 2 | [4,_] | |||||
| 15 | 17 | i < n2? | 1 | 2 | [4,_] | |||||
| 16 | 18 | Read b[i]; i++ | 2 | 2 | [4,5] | |||||
| 17 | 17 | i < n2? | 2 | 2 | [4,5] | |||||
| 18 | 5 | k = 0, i = 0 | 0 | 0 | ||||||
| 19 | 21 | i < n1? | 0 | 0 | ||||||
| 20 | 22 | c[k++] = a[i]; i++ | 1 | 1 | [1] |
Time and space complexity
Time: O(n1 + n2) · Space: O(n1 + n2). Each element of both arrays is read, copied and printed once, and the merged array holds n1 + n2 elements.
Common mistakes
- Resetting k to 0 before the second loop, which overwrites the elements of a instead of appending after them.
- Making c too small. It needs room for at least n1 + n2 elements (here 100 for two arrays of up to 50).
- Printing up to n1 or n2 instead of k, so part of the merged array is missed or garbage is shown.
Viva questions
Q1. Why is k initialised to 0, and what does it represent in this program?
k is the next free index in the merged array c, so it starts at 0 to fill from the first slot. After both copy loops it equals n1 + n2, which is why the print loop runs up to k.
Q2. What does c[k++] = a[i]; do? Is it the same as writing c[k] = a[i]; k++;?
It stores a[i] at position k of c and then increases k by one (post-increment uses the old value first). Yes, it is the same as the two separate statements.
Q3. Why is the size of array c declared as 100?
The two input arrays can each hold up to 50 elements, so the merged array may need up to 50 + 50 = 100 slots. A smaller c could overflow.
Q4. What happens if the user enters n1 = 0, or both sizes as 0?
The loops for that array run zero times, so nothing is read or copied. If both are 0, k stays 0 and the program prints only 'Merged array: ' with no elements.
Q5. What happens if the user enters a size greater than 50, or a negative size?
A size above 50 writes past the end of a or b, which is undefined behaviour and may crash or corrupt memory, since the program does not validate input. A negative size makes the loop condition false immediately, so the array is treated as empty.
Q6. Is the merged array sorted? How does this differ from the merge step of merge sort?
No, it is just a concatenation: all of a followed by all of b, in their original order. The merge step of merge sort compares the front elements of two sorted arrays and picks the smaller each time, producing a sorted result.
Q7. What is the time and space complexity of this program?
Time is O(n1 + n2) because each element is read once, copied once and printed once. Extra space is O(n1 + n2) for the third array c.
Q8. What would you change to merge the two arrays in sorted order, or to avoid the fixed size limit?
For sorted output, sort both arrays and use two indices i and j, always copying the smaller of a[i] and b[j] into c, then copy the leftovers. To remove the size limit, use malloc to allocate c (and a, b) with size n1 + n2 after reading the sizes.
Frequently asked questions
How do you merge two arrays in C?
Create a third array of size n1 + n2, copy all elements of the first array into it, then copy all elements of the second array starting from the next free index. A single index variable like k does this cleanly.
Why use c[k++] instead of c[i]?
The index i restarts at 0 for each array, but the elements of b must go after those of a. k keeps counting across both loops, so c[k++] always writes to the next free position.
Does this program sort the merged array?
No. It simply places the elements of a first and then the elements of b, in their original order. To get a sorted result you would have to sort c afterwards or use a sorted-merge method.