Merge Sort

(referrence: GeeksforGeeks)

MergeSort 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 merg() 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.

MergeSort(arr[], l,  r)
If r > l
     1. Find the middle point to divide the array into two halves:  
             middle m = l + (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.

Recursion

 1 class Solution {
 2     // Note here we need start and end pointers
 3     public static void mergeSort(int[] nums, int start, int end) {
 4         if (start < end) {
 5             int mid = (end - start) / 2 + start;
 6             mergeSort(nums, start, mid);
 7             mergeSort(nums, mid + 1, end);
 8             merge(nums, start, mid, end);
 9         }
10     }
11 
12     private void merge(int[] nums, int start, int mid, int end) {
13         int length1 = mid - start + 1;
14         int[] firstArray = new int[length1];
15         int length2 = end - mid;
16         int[] secondArray = new int[length2];
17 
18         // Copy data to two tmp arrays
19         for (int i = 0; i < length1; i++)
20             firstArray[i] = nums[i + start];
21         for (int i = 0; i < length2; i++)
22             secondArray[i] = nums[i + mid + 1];
23 
24         // Merge two arrays
25 
26         int tmp = 0, firstP = 0, secondP = 0;
27         while (firstP < length1 && secondP < length2) {
28             if (firstArray[firstP] < secondArray[secondP]) {
29                 nums[tmp] = firstArray[firstP];
30                 firstP++;
31             } else {
32                 nums[tmp] = secondArray[secondP];
33                 secondP++;
34             }
35             tmp++;
36         }
37 
38         while (firstP < length1) {
39             nums[tmp] = firstArray[firstP];
40             tmp++;
41             firstP++;
42         }
43 
44         while (secondP < length2) {
45             nums[tmp] = secondArray[secondP];
46             tmp++;
47             secondP++;
48         }
49     }
50 }

Time complexity for merge step is O(n), so whole time complexity T(n) = 2T(n / 2) + O(n) = O(n log (n)). Space cost is O(n).

Iteration

details explanation here

Key to the solution is to merge adjacent block in original array. Notice that width increases in the form of 2n.

 1 class Solution {
 2     public static void mergeSort(int[] a, int[] tmp) {
 3         int width;
 4         for (width = 1; width < a.length; width = 2 * width) {
 5             int i;
 6             for (i = 0; i < a.length; i = i + 2 * width) {
 7                 int start, mid, end;
 8                 start = i;
 9                 mid = i + width;
10                 end = i + 2 * width;
11                 merge(a, tmp, start, mid, end);
12             }
13 
14             for (i = 0; i < a.length; i++)
15                 a[i] = tmp[i];
16         }
17     }
18 
19     private static void merge(int[] a, int[] tmp, int start, int mid, int end) {
20         int i = start, j = mid, k = start;
21 
22         while (i < mid && j < end) {
23             if (a[i] < a[j])
24                 tmp[k++] = a[i++];
25             else
26                 tmp[k++] = a[j++];
27         }
28         while (i < mid)
29             tmp[k++] = a[i++];
30         while (j < end)
31             tmp[k++] = a[j++];
32     }
33 }

 

posted @ 2015-10-07 03:26  树獭君  阅读(286)  评论(0编辑  收藏  举报