-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
68 lines (54 loc) · 1.7 KB
/
Copy pathMergeSort.java
File metadata and controls
68 lines (54 loc) · 1.7 KB
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
64
65
66
import java.util.Arrays;
public class MergeSort {
public static Integer[] sort(Integer[] nums) {
return splitAndMerge(0, nums.length - 1, nums);
}
public static Integer[] splitAndMerge(int start, int end, Integer[] arr) {
// base case
int a;
if (end == start) {
try {
a = arr[start];
} catch (Error e) {
System.out.println("start: " + start + ", arr: " + Arrays.toString(arr));
}
Integer[] res = new Integer[] {arr[start]};
return res;
}
int mid = start + (int)((double)end - (double)start) / 2;
Integer[] left = splitAndMerge(start, mid, arr);
Integer[] right = splitAndMerge(mid + 1, end, arr);
return merge(left, right);
}
public static Integer[] merge(Integer[] leftList, Integer[] rightList) {
// copies array, inefficient space usage, could zipper lists for O(1) space
int currentLeft = 0;
int currentRight = 0;
Integer[] result = new Integer[leftList.length + rightList.length];
// advance L & R until all numbers have been added to new sorted array
for( int i = 0; i < result.length; i++) {
if (currentLeft == leftList.length) {
int right = rightList[currentRight];
result[i] = right;
currentRight++;
continue;
}
if (currentRight == rightList.length) {
int left = leftList[currentLeft];
result[i] = left;
currentLeft++;
continue;
}
int left = leftList[currentLeft];
int right = rightList[currentRight];
if (left < right || left == right) {
result[i] = left;
currentLeft++;
} else {
result[i] = right;
currentRight++;
}
}
return result;
}
}