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.

Example: If elements of array A = 1, 4, 6, 9, 15
Elements of array B = 2, 5, 8, 10
Merged array in ascending order = 1, 2, 4, 5, 6, 8, 9, 10, 15

Required knowledge

Basic C programming, If else, For loop, Array

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], arr3[MAX_SIZE];
    int size1, size2, size3;
    int i, j, k;
     
    /*
     * Read size of first array and elements in first array
     */
    printf("Enter the size of first array : ");
    scanf("%d", &size1);
    printf("Enter elements in first array : ");
    for(i=0; i<size1; i++)
    {
        scanf("%d", &arr1[i]);
    }
     
    /*
     * Reads size of second array and elements in second array
     */
    printf("\nEnter the size of second array : ");
    scanf("%d", &size2);
    printf("Enter elements in second array : ");
    for(i=0; i<size2; i++)
    {
        scanf("%d", &arr2[i]);
    }

    /* size of merged array is size_of_first_array + size_of_second_array */
    size3 = size1 + size2;
     
    /*
     * Merge two array in ascending order 
     */
    for(i=0, j=0, k=0; i<size3; i++)
    {
        /* 
         * If all elements of one array 
         * is merged to final array
         */
        if(j >= size1 || k >= size2)
        {
            break;
        }


        if(arr1[j] < arr2[k])
        {
            arr3[i] = arr1[j];
            j++;
        }
        else
        {
            arr3[i] = arr2[k];
            k++;
        }
    }

    /*
     * Merge the remaining elements of array
     */
    while(j < size1)
    {
        arr3[i] = arr1[j];
        i++;
        j++;
    }
    while(k < size2)
    {
        arr3[i] = arr2[k];
        i++;
        k++;
    }

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

    return 0;
} 


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


Program to merge two sorted array in descending order

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

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

int main()
{
    int arr1[100], arr2[100], arr3[200];
    int size1, size2, size3;
    int i, j, k;

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

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

    /* size of merged array is size_of_first_array + size_of_second_array */
    size3 = size1 + size2;

    /*
     * Merges both array in descending order
     */
    for(i=0, j=0, k=0; i<size3; i++)
    {
        /* 
         * If all elements of one array 
         * is merged to final array
         */ 
        if(j >= size1 || k >= size2)
        {
            break;
        }

        if(arr1[j] > arr2[k])
        {
            arr3[i] = arr1[j];
            j++;
        }
        else
        {
            arr3[i] = arr2[k];
            k++;
        }
    }

    /*
     * Merge the remaining elements of array
     */
    while(j < size1)
    {
        arr3[i] = arr1[j];
        i++;
        j++;
    }
    while(k < size2)
    {
        arr3[i] = arr2[k];
        i++;
        k++;
    }

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

    return 0;
} 


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

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

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


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

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment