-
Notifications
You must be signed in to change notification settings - Fork 94
/
MergeSort.java
63 lines (58 loc) · 1.65 KB
/
MergeSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package chapter10SortingAndSearching;
import java.util.Arrays;
/**
*
* Problem:Merge Sort, stable
*
* Time Complexity: O(NlogN) average and worst.
*
* Space Complexity: O(N)
*
*/
public class MergeSort {
public int[] mergeSort(int[] nums) {
int[] helper = new int[nums.length];
mergeSort(nums, helper, 0, nums.length - 1);
return nums;
}
private void mergeSort(int[] nums, int[] helper, int low, int high) {
if (low < high) {
int mid = low + (high - low) / 2;
mergeSort(nums, helper, low, mid);
mergeSort(nums, helper, mid + 1, high);
merge(nums, helper, low, mid, high);
}
}
private void merge(int[] nums, int[] helper, int low, int mid, int high) {
for (int i = low; i <= high; i++) {
helper[i] = nums[i];
}
int helperLeft = low;
int helperRight = mid + 1;
int cur = low;
// iterate through helper array. Compare the left and right half,
// copying back the smaller element from the two halves into the
// original array.
while (helperLeft <= mid && helperRight <= high) {
if (helper[helperLeft] <= helper[helperRight]) {
nums[cur] = helper[helperLeft];
helperLeft++;
} else {
nums[cur] = helper[helperRight];
helperRight++;
}
cur++;
}
// copy the rest of the left side of the array into the target array.
// The right side doesn't need to be copied because it's already there.
int remaining = mid - helperLeft;
for (int i = 0; i <= remaining; i++) {
nums[cur + i] = helper[helperLeft + i];
}
}
public static void main(String[] args) {
MergeSort m = new MergeSort();
int[] nums = { 1, 2, 3, 5, 2, 4, 6, 7 };
System.out.println(Arrays.toString(m.mergeSort(nums)));
}
}