Related Articles
Java Program for Merge Sort
• Difficulty Level : Hard
• Last Updated : 13 Feb, 2018

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.

## Java

 `/* Java program for Merge Sort */``class` `MergeSort``{``    ``// Merges two subarrays of arr[].``    ``// First subarray is arr[l..m]``    ``// Second subarray is arr[m+1..r]``    ``void` `merge(``int` `arr[], ``int` `l, ``int` `m, ``int` `r)``    ``{``        ``// Find sizes of two subarrays to be merged``        ``int` `n1 = m - l + ``1``;``        ``int` `n2 = r - m;`` ` `        ``/* Create temp arrays */``        ``int` `L[] = ``new` `int` `[n1];``        ``int` `R[] = ``new` `int` `[n2];`` ` `        ``/*Copy data to temp arrays*/``        ``for` `(``int` `i=``0``; i

Please refer complete article on Merge Sort for more details!

My Personal Notes arrow_drop_up