Merge Two Arrays in C

How to merge two arrays in the C programming language. To perform merge operations we need to know how to copy an array in C.

Merge Two Arrays in C Without Sorting

Example of merging of two int arrays,
Array1 = {10, 20, 30, 40, 50}
Array2 = {9, 18, 27, 36, 45}
Then the Result should be,
Merged Array = {10, 20, 30, 40, 50, 9, 18, 27, 36, 45}

Steps to merge two arrays in C,
a) Take two arrays, assume src1 and src2.
b) Calculate the size of both arrays, assume n1 and n2 are their sizes.
b) Declare a new array with the size of both arrays (n1 + n2).
c) Copy first array (src1) to new array from 0 to n1-1
d) Copy the second array (src2) to the new array from n1 to (n1+n2).

C program to Merge two array without sorting

#include <stdio.h>
// function to merge two array in 
// ascending order
void mergeSorted(int src1[], int src2[], 
  int newArr[], int n1, int n2, int n3)
{
  // copy src1 to new array
  for (int i = 0; i < n1; ++i)
  {
    newArr[i] = src1[i];
  }

  // copy src2 to new array
  for (int i = 0, j=n1; j < n3; ++i, ++j)
  {
    newArr[j] = src2[i];
  }
}

// main function
int main()
{
  // array which should be merged
  int src1[] = {10, 20, 30, 40, 50};
  int src2[] = {9, 18, 27, 36, 45};

  // calculate size
  int n1 = sizeof(src1)/sizeof(src1[0]);
  int n2 = sizeof(src2)/sizeof(src2[0]);

  // size of new array
  int n3 = n1 + n2;
      
  // create new array
  int newArr[n3];

  mergeSorted(src1, src2, newArr, n1, n2, n3);

  // display resultant array
  printf("New array = ");
  for (int i = 0; i < n3; i++) {
    printf("%d ",newArr[i]);
  }

  return 0;
}

Output:-

New array = 10 20 30 40 50 9 18 27 36 45

Merge two arrays in ascending order

Example-1 of merging of two int arrays in ascending order,
Array1 = {45, 18, 36, 27, 9}
Array2 = {40, 10, 20, 30, 50}
Then the Result should be,
Merged Array = {9, 10, 18, 20, 27, 30, 36, 40, 45, 50}

Example-2 of merging of two int arrays in ascending order,
Array1 = {1, 9, 10, 14, 15}
Array2 = {2, 3, 5, 11, 12}
Then the Result should be,
Merged Array = {1, 2, 3, 5, 9, 10, 11, 12, 14, 15}

Method1:- First Merge then Sort

In this method, we will use the previous approach to merge two arrays and then apply sorting algorithms on the resultant array to sort them.

#include <stdio.h>
// function to sort the array
void sort(int arr[], int n)
{
  // sort the merged array
  for(int i=0; i < n; i++)
  {
    for(int j=0; j < n-1; j++)
    {
      if(arr[j] > arr[j+1])
      {
        int temp = arr[j];
        arr[j] = arr[j+1];
        arr[j+1] = temp;
      }
    }
  }
}

// function to merge two array in 
// ascending order
void mergeSorted(int src1[], int src2[], 
  int newArr[], int n1, int n2, int n3)
{
  // copy src1 to new array
  for (int i = 0; i < n1; ++i)
  {
    newArr[i] = src1[i];
  }

  // copy src2 to new array
  for (int i = 0, j=n1; j < n3; ++i, ++j)
  {
    newArr[j] = src2[i];
  }

  // merge the new array
  sort(newArr, n3);
}

// main function
int main()
{
  // array which should be merged
  int src1[] = {45, 18, 36, 27, 9};
  int src2[] = {40, 10, 20, 30, 50};

  // calculate size
  int n1 = sizeof(src1)/sizeof(src1[0]);
  int n2 = sizeof(src2)/sizeof(src2[0]);

  // size of new array
  int n3 = n1 + n2;
      
  // create new array
  int newArr[n3];

  mergeSorted(src1, src2, newArr, n1, n2, n3);

  // display resultant array
  printf("New array = ");
  for (int i = 0; i < n3; i++) {
    printf("%d ",newArr[i]);
  }

  return 0;
}

Output:-

New array = 9 10 18 20 27 30 36 40 45 50

Time Complexity : O ( (n + m) (log(n + m)) )
Auxiliary Space : O ( (n + m) )

Method2:- The first sort then merge

In this method sort the both original array, and while merging insert them in appriorate position i.e. in ascending order.

#include <stdio.h>
// function to sort the array
void sort(int arr[], int n)
{
  // sort the merged array
  for(int i=0; i < n; i++)
  {
    for(int j=0; j < n-1; j++)
    {
      if(arr[j] > arr[j+1])
      {
        int temp = arr[j];
        arr[j] = arr[j+1];
        arr[j+1] = temp;
      }
    }
  }
}

// function to merge two array in 
// ascending order
void mergeSorted(int src1[], int src2[], 
  int newArr[], int n1, int n2)
{
  // sort both original array
  sort(src1, n1);
  sort(src2, n2);

  // merge two sorted arrays into newArr[] 
  int i = 0, j = 0, k = 0; 
  while (i < n1 && j < n2) 
  { 
    if (src1[i] <= src2[j]) 
    { 
      newArr[k++] = src1[i++]; 
    } 
    else 
    { 
      newArr[k++] = src2[j++]; 
    } 
  }     
  
  // merg remaining elements 
  // of src1[] (if any) 
  while (i < n1) 
  {
    newArr[k++] = src1[i++]; 
  }

  // merg remaining elements 
  // of src2[] (if any) 
  while (j < n2) 
  {   
    newArr[k++] = src2[j++]; 
  } 
}

// main function
int main()
{
  // array which should be merged
  int src1[] = {45, 18, 36, 27, 9};
  int src2[] = {40, 10, 20, 30, 50};

  // calculate size
  int n1 = sizeof(src1)/sizeof(src1[0]);
  int n2 = sizeof(src2)/sizeof(src2[0]);
      
  // create new array
  int newArr[n1+n2];

  mergeSorted(src1, src2, newArr, n1, n2);

  // display newArrultant array
  printf("New array = ");
  for (int i = 0; i < n1+n2; i++) {
    printf("%d ",newArr[i]);
  }

  return 0;
}

Output:-

New array = 9 10 18 20 27 30 36 40 45 50

Time Complexity : O (n log(n) + m log(m) + (n + m))
Space Complexity : O (n + m)

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply