0
0
CProgramBeginner · 2 min read

C Program to Merge Two Arrays with Output and Explanation

To merge two arrays in C, create a new array large enough to hold both, then copy elements from the first array followed by the second using a loop, like for (int i = 0; i < n1; i++) merged[i] = arr1[i]; for (int j = 0; j < n2; j++) merged[n1 + j] = arr2[j];.
📋

Examples

Inputarr1 = {1, 2}, arr2 = {3, 4}
OutputMerged array: 1 2 3 4
Inputarr1 = {5, 10, 15}, arr2 = {20, 25}
OutputMerged array: 5 10 15 20 25
Inputarr1 = {}, arr2 = {7, 8, 9}
OutputMerged array: 7 8 9
🧠

How to Think About It

To merge two arrays, think of putting all items from the first array into a new bigger box, then placing all items from the second array right after. This means you first copy each element from the first array, then continue copying elements from the second array into the new combined array.
📐

Algorithm

1
Get the size and elements of the first array.
2
Get the size and elements of the second array.
3
Create a new array with size equal to sum of both arrays' sizes.
4
Copy all elements from the first array into the new array.
5
Copy all elements from the second array into the new array after the first array's elements.
6
Print the merged array.
💻

Code

c
#include <stdio.h>

int main() {
    int arr1[] = {1, 2, 3};
    int arr2[] = {4, 5, 6};
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    int merged[n1 + n2];

    for (int i = 0; i < n1; i++) {
        merged[i] = arr1[i];
    }
    for (int j = 0; j < n2; j++) {
        merged[n1 + j] = arr2[j];
    }

    printf("Merged array: ");
    for (int k = 0; k < n1 + n2; k++) {
        printf("%d ", merged[k]);
    }
    return 0;
}
Output
Merged array: 1 2 3 4 5 6
🔍

Dry Run

Let's trace merging arr1 = {1, 2, 3} and arr2 = {4, 5, 6} through the code.

1

Initialize arrays and sizes

arr1 = {1, 2, 3}, n1 = 3; arr2 = {4, 5, 6}, n2 = 3; merged size = 6

2

Copy arr1 into merged

merged[0] = 1, merged[1] = 2, merged[2] = 3

3

Copy arr2 into merged after arr1

merged[3] = 4, merged[4] = 5, merged[5] = 6

4

Print merged array

Output: 1 2 3 4 5 6

Indexmerged[Index]
01
12
23
34
45
56
💡

Why This Works

Step 1: Create merged array

We make a new array with size equal to the sum of both arrays so it can hold all elements.

Step 2: Copy first array elements

We copy each element from the first array into the new array starting at index 0.

Step 3: Copy second array elements

We continue copying elements from the second array starting right after the last element of the first array.

Step 4: Print merged array

We print all elements of the merged array to show the combined result.

🔄

Alternative Approaches

Using dynamic memory allocation
c
#include <stdio.h>
#include <stdlib.h>

int main() {
    int arr1[] = {1, 2, 3};
    int arr2[] = {4, 5, 6};
    int n1 = 3, n2 = 3;
    int *merged = malloc((n1 + n2) * sizeof(int));
    for (int i = 0; i < n1; i++) merged[i] = arr1[i];
    for (int j = 0; j < n2; j++) merged[n1 + j] = arr2[j];
    printf("Merged array: ");
    for (int k = 0; k < n1 + n2; k++) printf("%d ", merged[k]);
    free(merged);
    return 0;
}
This approach uses dynamic memory to handle arrays of unknown size at runtime but requires manual memory management.
Merge and sort after combining
c
#include <stdio.h>

void sort(int arr[], int n) {
    for (int i = 0; i < n-1; i++) {
        for (int j = i+1; j < n; j++) {
            if (arr[i] > arr[j]) {
                int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
            }
        }
    }
}

int main() {
    int arr1[] = {3, 1, 2};
    int arr2[] = {6, 4, 5};
    int n1 = 3, n2 = 3;
    int merged[6];
    for (int i = 0; i < n1; i++) merged[i] = arr1[i];
    for (int j = 0; j < n2; j++) merged[n1 + j] = arr2[j];
    sort(merged, n1 + n2);
    printf("Merged and sorted array: ");
    for (int k = 0; k < n1 + n2; k++) printf("%d ", merged[k]);
    return 0;
}
This method merges then sorts the combined array, useful if you want the merged array sorted.

Complexity: O(n1 + n2) time, O(n1 + n2) space

Time Complexity

The program loops through both arrays once to copy elements, so time grows linearly with total elements.

Space Complexity

A new array is created to hold all elements, so space used is proportional to the sum of both arrays.

Which Approach is Fastest?

Simple copying is fastest; sorting after merging adds extra time. Dynamic allocation adds overhead but is flexible.

ApproachTimeSpaceBest For
Simple copyO(n1 + n2)O(n1 + n2)Fixed size arrays, fastest
Dynamic allocationO(n1 + n2)O(n1 + n2)Unknown sizes, flexible memory
Merge then sortO((n1 + n2)^2)O(n1 + n2)When merged array must be sorted
💡
Always ensure the merged array has enough space to hold all elements from both arrays.
⚠️
Forgetting to allocate enough space for the merged array causes overwriting or crashes.