Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one. See following C implementation for details.

MergeSort(arr[], l, r) 

If r > l

1. Find the middle point to divide the array into two halves: middle m = (l+r)/2

2. Call mergeSort for first half: Call mergeSort(arr, l, m)

3. Call mergeSort for second half: Call mergeSort(arr, m+1, r)

4. Merge the two halves sorted in step 2 and 3: Call merge(arr, l, m, r)

The following diagram from wikipedia shows the complete merge sort process for an example array {38, 27, 43, 3, 9, 82, 10}. If we take a closer look at the diagram, we can see that the array is recursively divided in two halves till the size becomes 1. Once the size becomes 1, the merge processes comes into action and starts merging arrays back till the complete array is merged.

merge sort

[ad type=”banner”]

C and C++

[pastacode lang=”c” manual=”%2F*%20C%20program%20for%20Merge%20Sort%20*%2F%0A%23include%3Cstdlib.h%3E%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20Merges%20two%20subarrays%20of%20arr%5B%5D.%0A%2F%2F%20First%20subarray%20is%20arr%5Bl..m%5D%0A%2F%2F%20Second%20subarray%20is%20arr%5Bm%2B1..r%5D%0Avoid%20merge(int%20arr%5B%5D%2C%20int%20l%2C%20int%20m%2C%20int%20r)%0A%7B%0A%20%20%20%20int%20i%2C%20j%2C%20k%3B%0A%20%20%20%20int%20n1%20%3D%20m%20-%20l%20%2B%201%3B%0A%20%20%20%20int%20n2%20%3D%20%20r%20-%20m%3B%0A%20%0A%20%20%20%20%2F*%20create%20temp%20arrays%20*%2F%0A%20%20%20%20int%20L%5Bn1%5D%2C%20R%5Bn2%5D%3B%0A%20%0A%20%20%20%20%2F*%20Copy%20data%20to%20temp%20arrays%20L%5B%5D%20and%20R%5B%5D%20*%2F%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20n1%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20L%5Bi%5D%20%3D%20arr%5Bl%20%2B%20i%5D%3B%0A%20%20%20%20for%20(j%20%3D%200%3B%20j%20%3C%20n2%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20R%5Bj%5D%20%3D%20arr%5Bm%20%2B%201%2B%20j%5D%3B%0A%20%0A%20%20%20%20%2F*%20Merge%20the%20temp%20arrays%20back%20into%20arr%5Bl..r%5D*%2F%0A%20%20%20%20i%20%3D%200%3B%20%2F%2F%20Initial%20index%20of%20first%20subarray%0A%20%20%20%20j%20%3D%200%3B%20%2F%2F%20Initial%20index%20of%20second%20subarray%0A%20%20%20%20k%20%3D%20l%3B%20%2F%2F%20Initial%20index%20of%20merged%20subarray%0A%20%20%20%20while%20(i%20%3C%20n1%20%26%26%20j%20%3C%20n2)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(L%5Bi%5D%20%3C%3D%20R%5Bj%5D)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bk%5D%20%3D%20L%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20i%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bk%5D%20%3D%20R%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20j%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20k%2B%2B%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Copy%20the%20remaining%20elements%20of%20L%5B%5D%2C%20if%20there%0A%20%20%20%20%20%20%20are%20any%20*%2F%0A%20%20%20%20while%20(i%20%3C%20n1)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20arr%5Bk%5D%20%3D%20L%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20i%2B%2B%3B%0A%20%20%20%20%20%20%20%20k%2B%2B%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Copy%20the%20remaining%20elements%20of%20R%5B%5D%2C%20if%20there%0A%20%20%20%20%20%20%20are%20any%20*%2F%0A%20%20%20%20while%20(j%20%3C%20n2)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20arr%5Bk%5D%20%3D%20R%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20j%2B%2B%3B%0A%20%20%20%20%20%20%20%20k%2B%2B%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20l%20is%20for%20left%20index%20and%20r%20is%20right%20index%20of%20the%0A%20%20%20sub-array%20of%20arr%20to%20be%20sorted%20*%2F%0Avoid%20mergeSort(int%20arr%5B%5D%2C%20int%20l%2C%20int%20r)%0A%7B%0A%20%20%20%20if%20(l%20%3C%20r)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Same%20as%20(l%2Br)%2F2%2C%20but%20avoids%20overflow%20for%0A%20%20%20%20%20%20%20%20%2F%2F%20large%20l%20and%20h%0A%20%20%20%20%20%20%20%20int%20m%20%3D%20l%2B(r-l)%2F2%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Sort%20first%20and%20second%20halves%0A%20%20%20%20%20%20%20%20mergeSort(arr%2C%20l%2C%20m)%3B%0A%20%20%20%20%20%20%20%20mergeSort(arr%2C%20m%2B1%2C%20r)%3B%0A%20%0A%20%20%20%20%20%20%20%20merge(arr%2C%20l%2C%20m%2C%20r)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20UTILITY%20FUNCTIONS%20*%2F%0A%2F*%20Function%20to%20print%20an%20array%20*%2F%0Avoid%20printArray(int%20A%5B%5D%2C%20int%20size)%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%3D0%3B%20i%20%3C%20size%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20A%5Bi%5D)%3B%0A%20%20%20%20printf(%22%5Cn%22)%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B12%2C%2011%2C%2013%2C%205%2C%206%2C%207%7D%3B%0A%20%20%20%20int%20arr_size%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%0A%20%20%20%20printf(%22Given%20array%20is%20%5Cn%22)%3B%0A%20%20%20%20printArray(arr%2C%20arr_size)%3B%0A%20%0A%20%20%20%20mergeSort(arr%2C%200%2C%20arr_size%20-%201)%3B%0A%20%0A%20%20%20%20printf(%22%5CnSorted%20array%20is%20%5Cn%22)%3B%0A%20%20%20%20printArray(arr%2C%20arr_size)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C AND C++” highlight=”” provider=”manual”/]

Java

[pastacode lang=”java” manual=”%2F*%20Java%20program%20for%20Merge%20Sort%20*%2F%0Aclass%20MergeSort%0A%7B%0A%20%20%20%20%2F%2F%20Merges%20two%20subarrays%20of%20arr%5B%5D.%0A%20%20%20%20%2F%2F%20First%20subarray%20is%20arr%5Bl..m%5D%0A%20%20%20%20%2F%2F%20Second%20subarray%20is%20arr%5Bm%2B1..r%5D%0A%20%20%20%20void%20merge(int%20arr%5B%5D%2C%20int%20l%2C%20int%20m%2C%20int%20r)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Find%20sizes%20of%20two%20subarrays%20to%20be%20merged%0A%20%20%20%20%20%20%20%20int%20n1%20%3D%20m%20-%20l%20%2B%201%3B%0A%20%20%20%20%20%20%20%20int%20n2%20%3D%20r%20-%20m%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Create%20temp%20arrays%20*%2F%0A%20%20%20%20%20%20%20%20int%20L%5B%5D%20%3D%20new%20int%20%5Bn1%5D%3B%0A%20%20%20%20%20%20%20%20int%20R%5B%5D%20%3D%20new%20int%20%5Bn2%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*Copy%20data%20to%20temp%20arrays*%2F%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn1%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20L%5Bi%5D%20%3D%20arr%5Bl%20%2B%20i%5D%3B%0A%20%20%20%20%20%20%20%20for%20(int%20j%3D0%3B%20j%3Cn2%3B%20%2B%2Bj)%0A%20%20%20%20%20%20%20%20%20%20%20%20R%5Bj%5D%20%3D%20arr%5Bm%20%2B%201%2B%20j%5D%3B%0A%20%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Merge%20the%20temp%20arrays%20*%2F%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Initial%20indexes%20of%20first%20and%20second%20subarrays%0A%20%20%20%20%20%20%20%20int%20i%20%3D%200%2C%20j%20%3D%200%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Initial%20index%20of%20merged%20subarry%20array%0A%20%20%20%20%20%20%20%20int%20k%20%3D%20l%3B%0A%20%20%20%20%20%20%20%20while%20(i%20%3C%20n1%20%26%26%20j%20%3C%20n2)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(L%5Bi%5D%20%3C%3D%20R%5Bj%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bk%5D%20%3D%20L%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20i%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bk%5D%20%3D%20R%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20j%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20k%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Copy%20remaining%20elements%20of%20L%5B%5D%20if%20any%20*%2F%0A%20%20%20%20%20%20%20%20while%20(i%20%3C%20n1)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bk%5D%20%3D%20L%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20i%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20k%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Copy%20remaining%20elements%20of%20R%5B%5D%20if%20any%20*%2F%0A%20%20%20%20%20%20%20%20while%20(j%20%3C%20n2)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bk%5D%20%3D%20R%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20j%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20k%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Main%20function%20that%20sorts%20arr%5Bl..r%5D%20using%0A%20%20%20%20%2F%2F%20merge()%0A%20%20%20%20void%20sort(int%20arr%5B%5D%2C%20int%20l%2C%20int%20r)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(l%20%3C%20r)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Find%20the%20middle%20point%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20m%20%3D%20(l%2Br)%2F2%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Sort%20first%20and%20second%20halves%0A%20%20%20%20%20%20%20%20%20%20%20%20sort(arr%2C%20l%2C%20m)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20sort(arr%20%2C%20m%2B1%2C%20r)%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Merge%20the%20sorted%20halves%0A%20%20%20%20%20%20%20%20%20%20%20%20merge(arr%2C%20l%2C%20m%2C%20r)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20A%20utility%20function%20to%20print%20array%20of%20size%20n%20*%2F%0A%20%20%20%20static%20void%20printArray(int%20arr%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(arr%5Bi%5D%20%2B%20%22%20%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println()%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20method%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20%7B12%2C%2011%2C%2013%2C%205%2C%206%2C%207%7D%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Given%20Array%22)%3B%0A%20%20%20%20%20%20%20%20printArray(arr)%3B%0A%20%0A%20%20%20%20%20%20%20%20MergeSort%20ob%20%3D%20new%20MergeSort()%3B%0A%20%20%20%20%20%20%20%20ob.sort(arr%2C%200%2C%20arr.length-1)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22%5CnSorted%20array%22)%3B%0A%20%20%20%20%20%20%20%20printArray(arr)%3B%0A%20%20%20%20%7D%0A%7D” message=”java” highlight=”” provider=”manual”/]

 

Python

[pastacode lang=”python” manual=”%23%20Python%20program%20for%20implementation%20of%20MergeSort%0A%20%0A%23%20Merges%20two%20subarrays%20of%20arr%5B%5D.%0A%23%20First%20subarray%20is%20arr%5Bl..m%5D%0A%23%20Second%20subarray%20is%20arr%5Bm%2B1..r%5D%0Adef%20merge(arr%2C%20l%2C%20m%2C%20r)%3A%0A%20%20%20%20n1%20%3D%20m%20-%20l%20%2B%201%0A%20%20%20%20n2%20%3D%20r-%20m%0A%20%0A%20%20%20%20%23%20create%20temp%20arrays%0A%20%20%20%20L%20%3D%20%5B0%5D%20*%20(n1)%0A%20%20%20%20R%20%3D%20%5B0%5D%20*%20(n2)%0A%20%0A%20%20%20%20%23%20Copy%20data%20to%20temp%20arrays%20L%5B%5D%20and%20R%5B%5D%0A%20%20%20%20for%20i%20in%20range(0%20%2C%20n1)%3A%0A%20%20%20%20%20%20%20%20L%5Bi%5D%20%3D%20arr%5Bl%20%2B%20i%5D%0A%20%0A%20%20%20%20for%20j%20in%20range(0%20%2C%20n2)%3A%0A%20%20%20%20%20%20%20%20R%5Bj%5D%20%3D%20arr%5Bm%20%2B%201%20%2B%20j%5D%0A%20%0A%20%20%20%20%23%20Merge%20the%20temp%20arrays%20back%20into%20arr%5Bl..r%5D%0A%20%20%20%20i%20%3D%200%20%20%20%20%20%23%20Initial%20index%20of%20first%20subarray%0A%20%20%20%20j%20%3D%200%20%20%20%20%20%23%20Initial%20index%20of%20second%20subarray%0A%20%20%20%20k%20%3D%20l%20%20%20%20%20%23%20Initial%20index%20of%20merged%20subarray%0A%20%0A%20%20%20%20while%20i%20%3C%20n1%20and%20j%20%3C%20n2%20%3A%0A%20%20%20%20%20%20%20%20if%20L%5Bi%5D%20%3C%3D%20R%5Bj%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bk%5D%20%3D%20L%5Bi%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20i%20%2B%3D%201%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bk%5D%20%3D%20R%5Bj%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20j%20%2B%3D%201%0A%20%20%20%20%20%20%20%20k%20%2B%3D%201%0A%20%0A%20%20%20%20%23%20Copy%20the%20remaining%20elements%20of%20L%5B%5D%2C%20if%20there%0A%20%20%20%20%23%20are%20any%0A%20%20%20%20while%20i%20%3C%20n1%3A%0A%20%20%20%20%20%20%20%20arr%5Bk%5D%20%3D%20L%5Bi%5D%0A%20%20%20%20%20%20%20%20i%20%2B%3D%201%0A%20%20%20%20%20%20%20%20k%20%2B%3D%201%0A%20%0A%20%20%20%20%23%20Copy%20the%20remaining%20elements%20of%20R%5B%5D%2C%20if%20there%0A%20%20%20%20%23%20are%20any%0A%20%20%20%20while%20j%20%3C%20n2%3A%0A%20%20%20%20%20%20%20%20arr%5Bk%5D%20%3D%20R%5Bj%5D%0A%20%20%20%20%20%20%20%20j%20%2B%3D%201%0A%20%20%20%20%20%20%20%20k%20%2B%3D%201%0A%20%0A%23%20l%20is%20for%20left%20index%20and%20r%20is%20right%20index%20of%20the%0A%23%20sub-array%20of%20arr%20to%20be%20sorted%0Adef%20mergeSort(arr%2Cl%2Cr)%3A%0A%20%20%20%20if%20l%20%3C%20r%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20Same%20as%20(l%2Br)%2F2%2C%20but%20avoids%20overflow%20for%0A%20%20%20%20%20%20%20%20%23%20large%20l%20and%20h%0A%20%20%20%20%20%20%20%20m%20%3D%20(l%2B(r-1))%2F2%0A%20%0A%20%20%20%20%20%20%20%20%23%20Sort%20first%20and%20second%20halves%0A%20%20%20%20%20%20%20%20mergeSort(arr%2C%20l%2C%20m)%0A%20%20%20%20%20%20%20%20mergeSort(arr%2C%20m%2B1%2C%20r)%0A%20%20%20%20%20%20%20%20merge(arr%2C%20l%2C%20m%2C%20r)%0A%20%0A%20%0A%23%20Driver%20code%20to%20test%20above%0Aarr%20%3D%20%5B12%2C%2011%2C%2013%2C%205%2C%206%2C%207%5D%0An%20%3D%20len(arr)%0Aprint%20(%22Given%20array%20is%22)%0Afor%20i%20in%20range(n)%3A%0A%20%20%20%20print%20(%22%25d%22%20%25arr%5Bi%5D)%2C%0A%20%0AmergeSort(arr%2C0%2Cn-1)%0Aprint%20(%22%5Cn%5CnSorted%20array%20is%22)%0Afor%20i%20in%20range(n)%3A%0A%20%20%20%20print%20(%22%25d%22%20%25arr%5Bi%5D)%2C” message=”python” highlight=”” provider=”manual”/]

Output:

Given array is 12 11 13 5 6 7 Sorted array is 5 6 7 11 12 13

Time Complexity: Sorting arrays on different machines. Merge Sort is a recursive algorithm and time complexity can be expressed as following recurrence relation.
T(n) = 2T(n/2) + \Theta(n)
The above recurrence can be solved either using Recurrence Tree method or Master method. It falls in case II of Master Method and solution of the recurrence is \Theta(nLogn).
Time complexity of Merge Sort is \Theta(nLogn) in all 3 cases (worst, average and best) as merge sort always divides the array in two halves and take linear time to merge two halves.

Auxiliary Space: O(n)

Algorithmic Paradigm: Divide and Conquer

Sorting In Place: No in a typical implementation

Stable: Yes

Applications of Merge Sort

  1. Merge Sort is useful for sorting linked lists in O(nLogn) time.In case of linked lists the case is different mainly due to difference in memory allocation of arrays and linked lists. Unlike arrays, linked list nodes may not be adjacent in memory. Unlike array, in linked list, we can insert items in the middle in O(1) extra space and O(1) time. Therefore merge operation of merge sort can be implemented without extra space for linked lists.In arrays, we can do random access as elements are continuous in memory. Let us say we have an integer (4-byte) array A and let the address of A[0] be x then to access A[i], we can directly access the memory at (x + i*4). Unlike arrays, we can not do random access in linked list. Quick Sort requires a lot of this kind of access. In linked list to access i’th index, we have to travel each and every node from the head to i’th node as we don’t have continuous block of memory. Therefore, the overhead increases for quick sort. Merge sort accesses data sequentially and the need of random access is low.
  2. Inversion Count Problem
  3. Used in External Sorting
[ad type=”banner”]

Categorized in:

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,