C Program to Merge the Elements of 2 Sorted Array

«
»

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

Problem Description

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

Problem Solution

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.

advertisement
Program/Source Code

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.

  1.     /*
  2.      * C Program to Merge the Elements of 2 Sorted Array
  3.      */
  4.  
  5.     #include <stdio.h>
  6.     void main()
  7.     {
  8.  
  9.         int array1[50], array2[50], array3[100], m, n, i, j, k = 0;
  10.         printf("\n Enter size of array Array 1: ");
  11.         scanf("%d", &m);
  12.  
  13.         printf("\n Enter sorted elements of array 1: \n");
  14.         for (i = 0; i < m; i++) 
  15.         {
  16.             scanf("%d", &array1[i]);
  17.         }
  18.  
  19.         printf("\n Enter size of array 2: ");
  20.         scanf("%d", &n);
  21.  
  22.         printf("\n Enter sorted elements of array 2: \n");
  23.         for (i = 0; i < n; i++) 
  24.         {
  25.             scanf("%d", &array2[i]);
  26.         }
  27.  
  28.         i = 0;
  29.         j = 0;
  30.  
  31.         while (i < m && j < n) 
  32.         {
  33.             if (array1[i] < array2[j]) 
  34.             {
  35.                 array3[k] = array1[i];
  36.                 i++;
  37.             }
  38.  
  39.             else 
  40.             {
  41.                 array3[k] = array2[j];
  42.                 j++;
  43.             }
  44.             k++;
  45.         }
  46.  
  47.         if (i >= m) 
  48.         {
  49.             while (j < n) 
  50.             {
  51.                 array3[k] = array2[j];
  52.                 j++;
  53.                 k++;
  54.             }
  55.         }
  56.  
  57.         if (j >= n) 
  58.         {
  59.             while (i < m)
  60.             {
  61.                 array3[k] = array1[i];
  62.                 i++;
  63.                 k++;
  64.             }
  65.         }
  66.  
  67.         printf("\n After merging: \n");
  68.         for (i = 0; i < m + n; i++) 
  69.         {
  70.             printf("\n%d", array3[i]);
  71.         }
  72.  
  73.     }
Program Explanation

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

advertisement
Runtime Test Cases
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.

advertisement

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn