Signup/Sign In

C Program To Merge Two Sorted Arrays

Learn C language tutorial

Merging two arrays means combining two separate arrays into one single array. For instance, if the first array consists of 3 elements and the second array consists of 5 elements then the resulting array consists of 8 elements. This resulting array is known as a merged array. Before moving forward, if you are not familiar with the concept of the array then, do check the article on Arrays in C.

We are given two sorted arrays and our task is to merge these two sorted arrays.

Input: First Array: 5 4 3 2 1

Second Array: 9 8 7 6 5

Output: Merged sorted Array: 9 8 7 6 5 5 4 3 2 1

Program 1: Merge Two Sorted Arrays

In this method, we will enter two sorted arrays as input and then merge them. After merging them, again we will sort the array and display the output.

Algorithm

  1. Start.
  2. Declare the size of the array.
  3. Declare the array.
  4. Initialize the array.
  5. While initializing the first array, copy the elements of the array to the merged array.
  6. While initializing the second array, copy the elements of the array to the merged array.
  7. Now sort the merged array.
  8. Display the resulting array.
  9. Stop.

In the below program firstly we declare and initialize two arrays. Now, merge these arrays and then sort the array.

#include <stdio.h>

int main()
{
    
    int n1,n2,n3;            //Array Size Declaration
    printf("\nEnter the size of first array ");
    scanf("%d",&n1);
    printf("\nEnter the size of second array ");
    scanf("%d",&n2);
    
    n3=n1+n2;
    printf("\nEnter the sorted array elements");
    int a[n1],b[n2],c[n3];     //Array Declaration
    for(int i=0;i<n1;i++)      //Array Initialized
    {
       scanf("%d",&a[i]);
       c[i]=a[i];
    }
    int k=n1;
    printf("\nEnter the sorted array elements");
    for(int i=0;i<n2;i++)      //Array Initialized
    {
        scanf("%d",&b[i]);
        c[k]=b[i];
        k++;
    }
    printf("\nThe merged array..\n");
    for(int i=0;i<n3;i++)
    printf("%d ",c[i]);        //Print the merged array
    
    printf("\nAfter sorting...\n");
    for(int i=0;i<n3;i++)         //Sorting Array
    {
        int temp;
        for(int j=i+1; j<n3 ;j++)
        {
            if(c[i]<c[j])
            {
                temp=c[i];
                c[i]=c[j];
                c[j]=temp;
            }
        }
    }   
    
    for(int i=0 ; i<n3 ; i++)       //Print the sorted Array 
    {
        printf(" %d ",c[i]);
    }
    return 0;   
}


Enter the size of first array 5
Enter the size of second array 5
Enter the sorted array elements 5 4 3 2 1
Enter the sorted array elements 9 8 7 6 5
The merged array..
1 2 3 4 5 9 8 7 6 5
After sorting...
9 8 7 6 5 5 4 3 2 1

Program 2: Merge Two Sorted Arrays

In this method, we will enter two arrays, and then we will call a function that will sort them. After that, we will merge the array and again sort them. Finally, we will display the output.

Algorithm

  1. Start
  2. Declare two arrays and initialize them.
  3. Declare a sort function that will sort the array elements in ascending order.
  4. After initializing the first two arrays, call the sort function.
  5. Now, merge the two arrays.
  6. Again, call the sort function.
  7. Print the resultant sorted array.
  8. End

In this method, we will use a function to solve the problem. Firstly, a function called the sort function is defined which will sort the array in order. Once an array is initialized we will call the sort function and sort that array. After sorting the two arrays it will merge them. Again sort function is called to sort the merged array.

#include <stdio.h>
void sort(int c[],int n3)         //Function to sort the elements of the array
{
    for(int i=0;i<n3;i++)         
    {
        int temp;
        for(int j=i+1; j<n3 ;j++)
        {
            if(c[i]>c[j])
            {
                temp=c[i];
                c[i]=c[j];
                c[j]=temp;
            }
        }
    }
}
int main()
{
    
    int n1,n2,n3;                   //Array size declaration
    printf("\nEnter the size of first array ");
    scanf("%d",&n1);
    printf("\nEnter the size of second array ");
    scanf("%d",&n2);
    
    n3=n1+n2;
    printf("\nEnter the array elements");
    int a[n1],b[n2],c[n3];                //Array Declaration
    for(int i=0;i<n1;i++)                 //Array Initialization
    {
       scanf("%d",&a[i]);
    }
    sort(a,n1);                             //Function call to sort the array
    int k=n1;
    printf("\nEnter the array elements");
    for(int i=0;i<n2;i++)                //Array Initialization
    {
        scanf("%d",&b[i]);
    }
    sort(b,n2);                          //Function call to sort the array
   
    for(int i=0;i<n1;i++)             //Merge two arrays
    {
        c[i]=a[i];
    }
    for(int j=0;j<n2;j++)
    {
        c[k]=b[j];
        k++;
    }
    printf("\nAfter sorting...\n");
    sort(c,n3);                               //Function call to sort the array
    for(int i=0 ; i<n3 ; i++)             //Print the resultant sorted array
    {
        printf(" %d ",c[i]);
    }
    return 0;  
}


Enter the size of first array 5
Enter the size of second array 5
Enter the array elements 36 27 68 9 20
Enter the array elements 71 92 53 24 15
After sorting...
9 15 20 24 27 36 53 68 71 92