C Program Merge and Sort Elements of 2 Different Arrays

This C Program merge and sort elements of 2 different arrays

Here is source code of the C Program to merge and sort elements of 2 different arrays. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Merge and Sort Elements of 2 different arrays
  3.  */
  4. #include <stdio.h>
  5.  
  6. void Merge(int * , int , int , int );
  7.  
  8. void MergeSort(int *array, int left, int right)
  9. {
  10.     int middle = (left+right)/2;
  11.     /* We have to sort only when left<right because when left=right it is anyhow sorted*/
  12.     if(left<right)
  13.     {
  14.         /* Sort the left part */
  15.         MergeSort(array, left, middle);
  16.         /* Sort the right part */
  17.         MergeSort(array, middle + 1, right);
  18.         /* Merge the two sorted parts */
  19.         Merge(array, left, middle, right);
  20.     }
  21. }
  22. /* Merge functions merges the two sorted parts */
  23. void Merge(int *array, int left, int middle, int right)
  24. {
  25.     /*to store sorted array*/
  26.     int tmp[right - left + 1];
  27.     int pos = 0, leftposition = left, rightposition = middle + 1;
  28.     while (leftposition <= middle && rightposition <= right)
  29.     {
  30.         if (array[leftposition] < array[rightposition])
  31.         {
  32.             tmp[pos++] = array[leftposition++];
  33.         }
  34.         else
  35.         {
  36.             tmp[pos++] = array[rightposition++];
  37.         }
  38.     }
  39.     while (leftposition <= middle)
  40.         tmp[pos++] = array[leftposition++];
  41.     while (rightposition <= right)
  42.         tmp[pos++] = array[rightposition++];
  43.     int i;
  44.     /* Copy back the sorted array to the original array */
  45.     for (i = 0; i < pos; i++)
  46.     {
  47.         array[i + left] = tmp[i];
  48.     }
  49.     return;
  50. }
  51. int main()
  52. {
  53.     int size;
  54.     printf("\n enter the size of an array");
  55.     scanf("%d", &size);
  56.     int array[size];
  57.     int i, j, k;
  58.     printf("\n enter the array elements");
  59.     for (i = 0; i < size; i++)
  60.     {
  61.         scanf("%d", &array[i]);
  62.     }
  63.     /* Calling this functions sorts the array */
  64.     MergeSort(array, 0, size - 1);
  65.     for (i = 0; i < size; i++)
  66.     {
  67.         printf("%d ", array[i]);
  68.     }
  69.     printf("\n");
  70.     return 0;
  71. }

$ cc pgm82.c
$ a.out
 
enter the size of an array10
 
enter the array elements-12
10
45
32
49
-58
69
38
98
34
-58 -12 10 32 34 38 45 49 69 98

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

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

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.