C++ Program to Implement Merge Sort

The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged in sorted order followed by combining them into a larger item. It is also very good for the worst cases because, for the worst-case, this algorithm also has lower time complexity.

The complexity of Merge Sort Technique

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

Input − the unsorted list: 14 20 78 98 20 45

Output − Array after Sorting: 14 20 20 45 78 98

Algorithm

Merge (array, left, middle, right)

Input: The data set array, left, middle and right index

Output: The merged list

mergeSort(array, left, right)

Input: An array of data, and lower and upper bound of the array

Output: The sorted Array

Example Code:

Output:

CPP Program to Implement Merge Sort

Pin It on Pinterest

Share This