Related Articles
How to make Mergesort to perform O(n) comparisons in best case?
• Difficulty Level : Medium
• Last Updated : 18 Sep, 2017

As we know, Mergesort is a divide and conquer algorithm that splits the array to halves recursively until it reaches an array of the size of 1, and after that it merges sorted subarrays until the original array is fully sorted. Typical implementation of merge sort works in O(n Log n) time in all three cases (best, average and worst).

We need to reduce the best case performance from O(n log n) to O(n).

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to consider the case when array is already sorted. Before merging, just check if arr[mid] > arr[mid+1], because we are dealing with sorted subarrays. This will lead us to the recursive relation T(n) = 2*T(n/2) + 1 which can be resolved by the master’s theorem, so T(n) = n.

Examples:

```Input : 1 2 3 4
Subarrays with size of 1:|1||2| |3||4|
Subarrays with size of 2: |1 2| |3 4|
Output : 1 2 3 4

Input : 1 2 3 4 5 6 7 8
Subarrays with size of 1: |1||2| |3||4| |5||6| |7||8|
Subarrays with size of 2: |1 2| |3 4| |5 6| |7 8|
Subarrays with size of 4: |1 2 3 4| |5 6 7 8|
Output : 1 2 3 4 5 6 7 8
```

 `// C program to implement merge sort that works ` `// in O(n) time in best case. ` `#include ` `#include ` ` `  `void` `merge(``int``* arr, ``int` `low, ``int` `mid, ``int` `high); ` ` `  `void` `mergesort(``int``* arr, ``int` `low, ``int` `high) ` `{ ` `    ``if` `(low < high) { ` `        ``int` `mid = (low + high) / 2; ` `        ``mergesort(arr, low, mid); ` `        ``mergesort(arr, mid + 1, high); ` ` `  `        ``// This is where we optimize for best ` `        ``// case. ` `        ``if` `(arr[mid] > arr[mid + 1]) ` `            ``merge(arr, low, mid, high); ` `    ``} ` `} ` ` `  `void` `merge(``int``* arr, ``int` `low, ``int` `mid, ``int` `high) ` `{ ` `    ``int` `i = low, j = mid + 1, k = 0; ` `    ``int``* temp = (``int``*)``calloc``(high - low + 1, ``sizeof``(``int``)); ` `    ``while` `((i <= mid) && (j <= high)) ` `        ``if` `(arr[i] < arr[j]) ` `            ``temp[k++] = arr[i++]; ` `        ``else` `            ``temp[k++] = arr[j++]; ` `    ``while` `(j <= high) ``// if( i>mid ) ` `        ``temp[k++] = arr[j++]; ` `    ``while` `(i <= mid) ``// j>high ` `        ``temp[k++] = arr[i++]; ` ` `  `    ``// copy temp[] to arr[] ` `    ``for` `(i = low, k = 0; i <= high; i++, k++) ` `        ``arr[i] = temp[k]; ` `    ``free``(temp); ` `} ` ` `  `int` `main() ` `{ ` `    ``int` `a[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; ` `    ``mergesort(a, 0, 7); ` `    ``for` `(``int` `i = 0; i < 8; i++) ` `        ``printf``(``"%d "``, a[i]); ` `    ``return` `0; ` `} `

Output:

``` 1 2 3 4 5 6 7 8
```

This article is contributed by Shlomi Elhaiani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.