C program to merge two sorted array

Write a C program to read elements in two array and merge elements of two array into third array. Elements of array can be merged either in ascending order or in descending order. Logic to merge two sorted array in C program.

Example

Input

Input first array elements: 1, 4, 6, 9, 15
Input second array elements: 2, 5, 8, 10

Output

Merged array in ascending order = 1, 2, 4, 5, 6, 8, 9, 10, 15

Required knowledge

Basic C programming, If else, For loop, Array

Must know - Program to read and print array elements

Logic to merge two sorted array

Below is the step by step descriptive logic to merge two sorted array.

  1. Input elements in two arrays and store them separately in some array variable. Say arr1 and arr2 stores first and second array elements respectively.
  2. Create another merge array with the size of sum of size of two input array. Say span class='code example'>mergeArray[mergeSize] will store the final merged elements (where mergeSize = sizeof arr1 + sizeof arr2.
  3. Initialize two variable index1 = 0 and index2 = 0. Both these variables will keep track of total merged elements from the given two array individually.
  4. Run a loop from 0 to mergeSize. The loop structure must look like for(index=0; index<mergeSize; i++).
  5. Inside the loop check for smallest element in two input array. Which is if(arr1[index1] < arr2[index2]) then mergeArray[index] = arr1[index1], otherwise store mergeArray[index] = arr2[index2];
  6. After loop merge the remaining array elements if any.

Program to merge two sorted array in ascending order

/**
 * C program to merge two sorted array in ascending order
 */

#include <stdio.h>
#define MAX_SIZE 100 //Maximum size of the array

int main()
{
    int arr1[MAX_SIZE], arr2[MAX_SIZE], mergeArray[MAX_SIZE];
    int size1, size2, mergeSize;
    int index, index1, index2;
    int i;
     
    /* Input size of the array */
    printf("Enter the size of first array : ");
    scanf("%d", &size1);

    /* Input elements in the array */
    printf("Enter elements in first array : ");
    for(i=0; i<size1; i++)
    {
        scanf("%d", &arr1[i]);
    }
     
    /* Input size of second array */
    printf("\nEnter the size of second array : ");
    scanf("%d", &size2);

    /* Input elements in second array */
    printf("Enter elements in second array : ");
    for(i=0; i<size2; i++)
    {
        scanf("%d", &arr2[i]);
    }

    mergeSize = size1 + size2;
     
    /*
     * Merge two array in ascending order 
     */
    index1 = 0;
    index2 = 0;
    for(index=0; index<size3; index++)
    {
        /* 
         * If all elements of one array 
         * is merged to final array
         */
        if(index2 >= size1 || index3 >= size2)
        {
            break;
        }


        if(arr1[index2] < arr2[index3])
        {
            mergeArray[index] = arr1[index2];
            j++;
        }
        else
        {
            mergeArray[index] = arr2[index2];
            k++;
        }
    }

    /*
     * Merge the remaining elements of array
     */
    while(index1 < size1)
    {
        mergeArray[index] = arr1[index1];
        index++;
        index1++;
    }
    while(index2 < size2)
    {
        mergeArray[index] = arr2[index2];
        index++;
        index2++;
    }


    /* 
     * Print merged array
     */
    printf("\nArray merged in ascending order : ");
    for(i=0; i<size3; i++)
    {
        printf("%d\t", mergeArray[i]);
    }

    return 0;
}

Important note: In order to merge array in ascending or descending order both array elements must be sorted in ascending or descending order.

Output

Enter the size of first array : 5
Enter elements in first array : 1 4 6 9 15

Enter the size of second array : 4
Enter elements in second array : 2 5 8 10

Array merged in ascending order : 1      2      4      5      6      8      9      10      15

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>