Write a C program to merge two sorted array in ascending order

C Merge two sorted array in ascending order


Write a C program to merge two sorted array in ascending order. Here’s simple program to merge two sorted array in ascending order in C Programming Language.


What is an Array ?


Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.


Here is source code of the C program to merge two sorted array in ascending order. The C program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :

/*  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("\nEnter elements in first array : \n");
    for(i=0; i<size1; i++)
    {
        printf("\nEnter %d element in first array : ",i+1);
        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("\nEnter elements in second array : \n");
    for(i=0; i<size2; i++)
    {
        printf("\nEnter %d element in second array : ",i+1);
        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 ", arr3[i]);
    }

    return 0;
}

OUTPUT : :


Enter the size of first array : 4

Enter elements in first array :

Enter 1 element in first array : 1

Enter 2 element in first array : 2

Enter 3 element in first array : 3

Enter 4 element in first array : 4

Enter the size of second array : 4

Enter elements in second array :

Enter 1 element in second array : 5

Enter 2 element in second array : 6

Enter 3 element in second array : 7

Enter 4 element in second array : 8

Array merged in ascending order :  1  2  3  4  5  6  7  8

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

Leave a Reply