C++ Program to Implement Merge Sort



The merge sort technique is based on the divide and conquer technique. We divide the whole data set into smaller parts and merge them into a larger piece in sorted order. It is also very effective for worst cases because this algorithm has lower time complexity for worst cases too.

In this article, we have an unsorted array of integers and our task is to implement the merge sort to sort the unsorted array.

Merge sort

Steps to to Implement Merge Sort

We will be using the steps mentioned below to implement the merge sort algorithm:

  • The first step is to find the midpoint of the array. Divide the array into two sub-arrays i.e. left and right sub-arrays using the middle index of the array.
  • We use recursion to keep dividing the array into sub-arrays until each sub-array contains only 1 element. In the example code given below the dividing part of the array is implemented in the mergeSort() function.
  • Then we compare each left and right sub-arrays and merge the sub-arrays in a sorted manner.
  • The above process is repeated until all the sub-arrays have been merged into a single array in a sorted manner. In the code given below the merging process is implemented in the merge() function.
  • Then we used the display() function to display the sorted array using merge sort.

C++ Program to Implement Merge Sort

Here is an example using the above steps to implement the merge sort in C++:

#include <iostream>
using namespace std;

//Displays the array
void display(int *array, int size) {    
   for(int i = 0; i < size; i++)
      cout << array[i] << " ";
   cout << endl;
}

//Function to merge the sub-arrays in sorted manner
void merge(int *array, int l, int m, int r) {   
   int i, j, k;
   int nl = m - l + 1;
   int nr = r - m;

   int larr[nl], rarr[nr];

   for(i = 0; i < nl; i++)
      larr[i] = array[l + i];
   for(j = 0; j < nr; j++)
      rarr[j] = array[m + 1 + j];

   i = 0; j = 0; k = l;

   while(i < nl && j < nr) {
      if(larr[i] <= rarr[j]) {
         array[k++] = larr[i++];
      } else {
         array[k++] = rarr[j++];
      }
   }

   while(i < nl)
      array[k++] = larr[i++];
   while(j < nr)
      array[k++] = rarr[j++];
}

//Recursive function to divide the array into sub-arrays
void mergeSort(int *array, int l, int r) {  
   if(l < r) {
      int m = l + (r - l) / 2;
      mergeSort(array, l, m);
      mergeSort(array, m + 1, r);
      merge(array, l, m, r);    //Merging the sorted sub arrays
   }
}

int main() {
   int arr[] = {25, 90, 6, 10, 5, 45, 35};
   int n = sizeof(arr) / sizeof(arr[0]);

   cout << "Array before Sorting: ";
   display(arr, n);

   mergeSort(arr, 0, n - 1);

   cout << "Array after Sorting: ";
   display(arr, n);

   return 0;
}

The output of the above code is:

Array before Sorting: 25 90 6 10 5 45 35 
Array after Sorting: 5 6 10 25 35 45 90 

Complexity of Merge Sort Technique

The time and space complexity of the merge sort is mentioned below:

  • Time Complexity: O(n log n) for all cases.

  • Space Complexity: O(n)

Updated on: 2025-04-15T15:26:32+05:30

45K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements