Latest :

C Program Merge Two Sorted Arrays – 3 Ways | C Programs

The problem statement here is, to write a C program to merge two sorted array in sorted order. To implement this, we’ll require the number of elements or sizes of the the two arrays along with the array elements in sorted order as our input. The desired output in the end is a single array which is the combination of two input arrays in sorted order. Check out the program below, C Program to merge two arrays without sorting.

C Program Merge Two Sorted Arrays Using Function

The first step here is to gather all the required inputs. For this, we can make use of scanf() function to read any primitive datatype input at runtime. This predefined function determines the type of input based on the format specifier we given within the function.

As all our inputs are of integer type, we will be giving ‘%d’ as our format specifier. So, first we’ll be reading the sizes of two arrays (n1,n2) and then, create the two arrays(array1,array2) of the respective sizes. Then, we iterate through individual array to read the elements or data values.

int n1,n2,i;

printf(“\nEnter the size of Array 1 : “);

scanf(“%d”,&n1);

printf(“\nEnter the size of Array 2 : “);

scanf(“%d”,&n2);

int array1[n1],array2[n2],array3[n1+n2];

printf(“\nEnter the %d sorted elements in Array1: “,n1);

for(i=0;i<n1;i++) {

scanf(“%d”,&array1[i]);

}

printf(“\nEnter the %d sorted elements in Array2: “,n2);

for(i=0;i<n2;i++) {

scanf(“%d”,&array2[i]);

}

After reading all the inputs, we also create a third array in which we’ll store the elements when merging the two inputs array in sorted order. This third array (array3) is of size n1+n2. Later, we make a function call to a user-defined function (mergeTwoSortedArrays) and pass all the inputs along with this third empty array as parameter.

This function contains the logic behind merging the two sorted arrays.

mergeTwoSortedArrays(array1,array2,array3,n1,n2);

In this function, we iterate until we reach the end of one of the two arrays. Initially we start from 0th index of both the arrays and compare the two elements. The value which is the smallest is stored into the third array (a3) and we increment the index of both the third array (a3) and the array from which the element inserted belonged to. Merge two sorted arrays without extra space.

Output:

Merge two sorted arrays C Program Standard Method

In the above method, we have seen the logic to merge the two sorted arrays by making use of functions. The advantages this has is that, functions make the code reusable and also enhances its readability.

If same logic is required later on in the code then, if the logic is placed within a function, we can just make a function call to this function and pass the necessary parameters thereby, reusing the code.

Also, since everything is placed in different function block on the basis of its functionality, the code becomes lot cleaner and finding some part of the code becomes easier.

As we can see above, we have split the code into two parts. The main method is taking care of all the input and output operations whereas, the user-defined function is taking care of the logic to sort the two arrays.

This way, we have made the logic of the code reusable and also improved the readability of the code.

But, if we are sure that, we do not require the use of the same logic elsewhere in the code then, there is no harm in write it all within the main method itself.

Since, both the time complexity and space complexity is the same for both as the same logic is being made use of, we can write the entire code within main method itself as shown below:

x

Check Also

C Program To Check A Number Is Negative, Positive Or Zero | C Programs

C program to check whether a number is negative, positive or zero – In this ...