This is a C Program merge the elements of 2 sorted array.

This problem will implement two one-dimentional arrays and will sort them separately. Then these sorted arrays are merged into one single sorted array.

1. Create two arrays of some fixed size and define their elements in sorted fashion.

2. Take two variables i and j, which will be at the 0th position of these two arrays.

3. Elements will be compared one by one using i and j in for loop, and whichever element is smaller than the other, that element will get inserted to final array and the position(either i or j) will move by one, whereas the other array’s track position will remain in that same place.

4. Above work will be done till we reach the end of either array. After that, one of the array whose elements are still to be added, its elements will get straightaway added to the final array.

Here is source code of the C Program to merge the elements of 2 sorted array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

`/*`

`* C Program to Merge the Elements of 2 Sorted Array`

`*/`

`#include <stdio.h>`

void main()

`{`

int array1[50], array2[50], array3[100], m, n, i, j, k = 0;

printf("\n Enter size of array Array 1: ");

scanf("%d", &m);

printf("\n Enter sorted elements of array 1: \n");

for (i = 0; i < m; i++)

`{`

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

`}`

printf("\n Enter size of array 2: ");

scanf("%d", &n);

printf("\n Enter sorted elements of array 2: \n");

for (i = 0; i < n; i++)

`{`

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

`}`

i = 0;

j = 0;

while (i < m && j < n)

`{`

if (array1[i] < array2[j])

`{`

array3[k] = array1[i];

`i++;`

`}`

`else`

`{`

array3[k] = array2[j];

`j++;`

`}`

`k++;`

`}`

if (i >= m)

`{`

while (j < n)

`{`

array3[k] = array2[j];

`j++;`

`k++;`

`}`

`}`

if (j >= n)

`{`

while (i < m)

`{`

array3[k] = array1[i];

`i++;`

`k++;`

`}`

`}`

printf("\n After merging: \n");

for (i = 0; i < m + n; i++)

`{`

printf("\n%d", array3[i]);

`}`

`}`

1. Declare 2 1D arrays of some fixed size, then take size of the arrays from user and define all the elements of the array according to the size in sorted fashion.

2. Take two variables, i and j as iterators which will track the position of elements in arrays.

3. Running a while loop till we reach the end of either array, the element at ith and jth position of two arrays are compared.

4. The smaller element gets inserted into final array (third array, whose size is the sum of the size of these two arrays) and the track position gets incremented by 1.

5. This process continues, till we reach the end of either array.

6. After finishing the loop above, one of the array’s tracker(i.e either i or j) will not be at the last position of the corresponding array, in that case we will have to add all the remaining elements of that array to the final array as it is one by one

Enter size of array Array 1: 4 Enter sorted elements of array 1: 12 18 40 60 Enter size of array 2: 4 Enter sorted elements of array 2: 47 56 89 90 After merging: 12 18 40 47 56 60 89 90

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms.