Introduction
In this blog we will discuss the approach to merge two sorted array in ascending order. Before jumping on to the approach to the problem, let us first understand the problem.

Also see: C Static Function and Short int in C Programming
Problem Statement
The primary goal is to merge two sorted arrays in ascending order and display the results as output.
Combining two arrays into a single array is known as merging two arrays. For example, if the first array has 5 elements and the second array has 4, the resultant array will have 9 elements. A merged array is the result of this process.
If you are unfamiliar with the concept of an array, let's see the example :
Input: 1st Array : 9 8 4 1 5 2nd Array 2: 2 0 3 6 Output : The merged sorted Array : 0 1 2 3 4 5 6 8 9 |

Approach 1:🧑🎓
Let's look at step-by-step descriptive logic to merge two sorted arrays in ascending order.
Algorithm
1. Start the program
2. Input the length of both the arrays.
3. Input the arrays elements from user.
4. Copy the elements of the first array to the merged array when initialising it.
5. Copy the elements of the second array to the merged array while initialising the second array.
6. Sort the merged array now.
7. Display the merged array.
8. The program ends here.
Implementation
|
Output :
Time complexity: (n+m)^2 since we are applying insertion sort on the merged array.
Auxiliary Space: O(n+m)
You can also read about dynamic array in c.
Approach 2:🧑🎓
Now for the second approach to merge two sorted arrays in ascending order, we need to fill the third array by comparing and inserting the smaller element one by one from the given sorted arrays. Let's look at step-by-step descriptive logic
for this approach.
Algorithm
1. Enter the two sorted arrays.
2. Find the size of the provided arrays and put them together, then declare a third array of the same size.
3. If the first array has the smaller element (i.e. array1[i] < array2[j]), fill the third array with the first array's element (i.e. array3[k++] = array1[i++]), otherwise with the second array's element (i.e. array3[k++] = array2[j++]).
4. Print the output which will be the third array .
Implementation
|
Output
Within the same expression, we are initialising the third array with values and incrementing the indexes.
The program has an O(n1+n2) time and space complexity, where n1 and n2 are the number of elements in the two arrays, respectively.
Refer to know about : Topological sort
Frequently Asked Questions
To merge two sorted lists, how many comparisons would be required in the worst case for merge sort?
In the worst case, m+n-1 comparisons are required to merge two lists of size m and n.
What is the most best case for merging the two sorted arrays into one sorted array?
O(m + n), where 'm' is the array's size of 'arr1', and 'n' is the array's size of 'arr2'. It takes O(m + n) time to merge the elements of 'arr1' and 'arr2' in 'arr3.'
In C, why do we return 0?
C language return's value of 0 in the main function indicates that the program ran successfully. The return 1 in the main function shows that the program did not run correctly and that an error occurred.
In the C programming language, which loop is the fastest?
In C programming, the Do-While loop is the fastest loop.