Merge Two Sorted Arrays in C Language

program-to-merge-two-sorted-arrays-in-c-language

Merge Two Sorted Arrays in C Program Description:

We have covered the Merging of Two unsorted arrays in our earlier articles. In today’s article, We will look at a Program to Merge Two Sorted Arrays in C Programming Language. The Program should accept two sorted arrays from the user and merge two sorted arrays input by the user and output a single sorted array.

Here is the example input and Output of the program.

Excepted Input and Output:

Input:

Enter desired size for array1 (1-100): 10

Please enter array elements(10) in Ascending Order : 100 200 300 400 500 600 666 700 800 900

Enter desired size for array2 (1-100): 10

Please enter array elements(10) in Ascending Order : 111 222 333 444 555 777 888 999 1111 2222

Output:

Array-1: 100 200 300 400 500 600 666 700 800 900

Array-2: 111 222 333 444 555 777 888 999 1111 2222

Sorted Merged Array: 100 111 200 222 300 333 400 444 500 555 600 666 700 777 800 888 900 999 1111 2222

So when user provided two sorted arrays 100 200 300 400 500 600 666 700 800 900 and 111 222 333 444 555 777 888 999 1111 2222, Then the Program merged them in sorted order 100 111 200 222 300 333 400 444 500 555 600 666 700 777 800 888 900 999 1111 2222

📢This Program is Part of the Arrays Practice Programs Series.

Prerequisites:

It is recommended to know the basics of the Arrays and Functions in C language to better understand this program. Please check out following articles to learn more about them

Merge Two Sorted Arrays in C Program Explanation:

Here is the step-by-step explanation of the Merge Two Sorted Arrays in C Program.

  1. Declare two arrays arr1[100] and arr2[100] as the input arrays. Also create a third array which is going to store the Merged Array. So declare sMergedArr[200], The sMergedArr can hold 200 elements, which is equal to the arr1+arr2 array’s max sizes. It is important for the sMergedArr to have the enough size to store the arr1 and arr2 arrays.
  2. Define two helper functions called read() function and display() function
    • Both these functions take two formal arguments which are arr[] array and it’s size.
    • The read() function takes the input from the user and updates the array elements
    • The display() function prints the array elements on the console.
  3. Now, We need to take the two arrays from the user. (To be precise, Array size and Array elements)
  4. Get the arr1 array size and elements from the user.
    • Call the read() function to store the elements in arr1
  5. Similarly, get the arr2 array size and elements from the user.
  6. Once we got the both arrays( arr1 and arr2). Print the arrays on the console by calling the display() funciton.
    • display(arr1, size1);
    • display(arr2, size2);
  7. Now, We need to Merge the Two Sorted Arrays.
    • Initialize two variables i and j with zero(0). The i and j will keep track of the index of the arr1 and arr2 arrays respectively.
    • Use a For Loop to Iterate over the sMergedArr array. (from k=0 to k<(size1+size2)), For each iteration,
      • Compare the arr1[i] and arr2[j] and copy the smallest number to the third array (i.e sMergedArr[k])
      • So check the elements at the arr1[i] and arr2[j], If the arr1[i] is smaller than the arr2[j], Then insert the arr1[i] at sMergedArr[k] and increment the i and k
      • If the arr2[j] is smaller than the arr1[i], Then copy the arr2[j] to sMergedArr[k] and Increment the j and k.
      • If any of the arrays is reached the end, Then we need to copy the remaining elements directly to the third array.
      • If the i is reached the size1 of the arr1, Then copy the remaining elements of the arr2 to sMergedArr – if(i >= size1)
      • Similarly, If the j is reached the size( size2) of the arr2, Then copy the remaining elements of the arr1 to sMergedArr – if (j >= size2)
  8. Once the above Loop is completed, Then the third array i.e sMergedArr will contain the Merged Array in Sorted order.
  9. Display the Merged array on the console by calling the display() function – display(sMergedArr, tSize);. where tSize = size1+size2;
  10. Stop the program.

Program to Merge Two Sorted Arrays in C Language:

Here is the Program to Merge Two Sorted Arrays in C language and produce a Merged Sorted Array.

Note that we have defined two function read() and display() with two formal arguments.

Program Output:

Let’s compile and run the program using GCC compiler (Any compiler)

Compile the Program

$ gcc merge-two-sorted-arr.c

Run the program

Example 1: When first array ( arr1) and second array( arr2) are of same size.

merge-two-sorted-arrays-program-output-ex-1

Example 2: When the first Array ( arr1) is larger than the Second array( arr2).

As you can see from the above outputs, The program is able to Merge the two sorted arrays and generated the sorted array.

Let’s see another example, Where the first array( arr1) size is smaller than the second array( arr2).

Example 3: When the First Array( arr1) size is smaller than the Second Array( arr2).

Finally, Let’s look at another example when the array size went out of the bounds.

Example 4: When the array size is out of bounds.

merge-two-sorted-arrays-in-c-program-output

As we can see from the above outputs, As excepted the program is generating the an error message( Invalid Size, Try again) on invalid size.

Related Array Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Merge Two Sorted Array and Generate Sorted Array […]

Leave a Reply