The merge sort algorithm closely follows the divide-and-conquer paradigm. Intuitively,
it operates as follows.
Divide: Divide the n-element sequence to be sorted into two subsequences of n=2
elements each.
Conquer: Sort the two subsequences recursively using merge sort.
Combine: Merge the two sorted subsequences to produce the sorted answer.
The recursion “bottoms out” when the sequence to be sorted has length 1, in which
case there is no work to be done, since every sequence of length 1 is already in
sorted order.
The key operation of the merge sort algorithm is the merging of two sorted
sequences in the “combine” step.
MERGE-SORT (A, l, r)
1 if l < r
2 mid = [ (l + r) / 2 ]
3 SORT (A, l , m )
4 SORT (A, m + 1, r)
5 MERGE (A, l, m, r)
/** * Created by Ali on 9/21/2016. */
class MergeSort { public static void main(String args[]) { int arr[] = {12, 11, 13, 5, 6, 7}; System.out.println("Given Array"); printArray(arr); MergeSort ob = new MergeSort(); ob.sort(arr, 0, arr.length-1); System.out.println("\nSorted array"); printArray(arr); }
// 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) { int n1 = m - l + 1; int n2 = r - m; int[] L = new int[n1]; int[] R = new int[n2]; for (int i = 0; i < n1; i++) { L[i] = arr[l + i]; } for (int j = 0; j < n2; j++) { R[j] = arr[m + 1 + j]; } int k = l; int i=0 , j=0; while(i < n1 && j < n2) { if(L[i] < R[j]) { arr[k++] = L[i++]; }else { arr[k++] = R[j++]; } } while(i < n1){ arr[k++] = L[i++]; } while(j < n2){ arr[k++] = R[j++]; } } void sort(int arr[], int l, int r){ if(l < r) { int m = (l+r)/2; sort(arr, l , m); sort(arr, m+1, r); merge(arr, l, m, r); } } private static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }
Time complexity of Merge Sort is O(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
Stable: Yes
Applications of Merge Sort
- 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.