C program to sort an array by using merge sort

An array is a group of related data items which share’s a common name. A particular value in an array is identified with the help of its "index number".
The syntax for declaring an array is as follows −
datatype array_name [size];
For example,
float marks [50]
It declares ‘marks’ to be an array containing 50 float elements.
int number[10]
It declares the ‘number’ as an array to contain a maximum of 10 integer constants.
Each element is identified by using an "array index".
Accessing array elements is easy by using the array index.
The logic we use for merge sort is as follows −
void MergeSort(int *array, int left, int right){
   int middle = (left+right)/2;
   if(left<right){
      //Sorting the left part
      MergeSort(array, left, middle);
      //Sorting the right part
      MergeSort(array, middle + 1, right);
      // Merge the two parts
      Merge(array, left, middle, right);
   }
}
The logic for merging all the elements is as follows −
void Merge(int *array, int left, int middle, int right){
   int tmp[right - left + 1];
   int pos = 0, leftposition = left, rightposition = middle + 1;
   while (leftposition <= middle && rightposition <= right){
      if (array[leftposition] < array[rightposition]){
         tmp[pos++] = array[leftposition++];
      }else{
         tmp[pos++] = array[rightposition++];
      }
   }
   while (leftposition <= middle)
      tmp[pos++] = array[leftposition++];
   while (rightposition <= right)
      tmp[pos++] = array[rightposition++];
   int i;
   for (i = 0; i < pos; i++){
      array[i + left] = tmp[i];
   }
   return;
}
Following is the C program to merge sort −
 Live Demo
#include <stdio.h>
void Merge(int * , int , int , int );
void MergeSort(int *array, int left, int right){
   int middle = (left+right)/2;
   if(left<right){
      //Sorting the left part
      MergeSort(array, left, middle);
      //Sorting the right part
      MergeSort(array, middle + 1, right);
      // Merge the two parts
      Merge(array, left, middle, right);
   }
}
void Merge(int *array, int left, int middle, int right){
   int tmp[right - left + 1];
   int pos = 0, leftposition = left, rightposition = middle + 1;
   while (leftposition <= middle && rightposition <= right){
      if (array[leftposition] < array[rightposition]){
         tmp[pos++] = array[leftposition++];
      }
      else{
         tmp[pos++] = array[rightposition++];
      }
   }
   while (leftposition <= middle)
      tmp[pos++] = array[leftposition++];
   while (rightposition <= right)
      tmp[pos++] = array[rightposition++];
   int i;
   for (i = 0; i < pos; i++){
      array[i + left] = tmp[i];
   }
   return;
}
int main(){
   int size;
   printf("\n enter size of array:");
   scanf("%d", &size);
   int array[size];
   int i, j, k;
   printf("\n enter the elements in an array:");
   for (i = 0; i < size; i++){
      scanf("%d", &array[i]);
   }
   MergeSort(array, 0, size - 1);//calling sort function
   for (i = 0; i< size; i++){
      printf("%d ", array[i]);
   }
   printf("\n");
   return 0;
}
When the above program is executed, it produces the following result −
enter size of array:10
enter the elements in an array:
2
-10
34
-3
45
67
-89
34
23
67
-89 -10 -3 2 23 34 34 45 67 67