-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.java
60 lines (45 loc) · 1.31 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
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
public class MergeSort {
public static void main(String[] args) {
List<Integer> example = Arrays.asList(new Integer[]{5,3,1,4,2});
System.out.println("before: " + example);
example = mergeSort(example);
System.out.println("after: " + example);
}
public static <T extends Comparable<? super T>> List<T> mergeSort(List<T> list) {
if (list.size() <= 1) {
return list;
}
int mid = list.size() / 2;
List<T> left = list.subList(0, mid);
List<T> right = list.subList(mid, list.size());
left = mergeSort(left);
right = mergeSort(right);
return merge(left, right);
}
public static <T extends Comparable<? super T>> List<T> merge(List<T> left, List<T> right) {
int leftIndex = 0;
int rightIndex = 0;
List<T> sorted = new ArrayList<T>();
while ( (leftIndex < left.size()) && (rightIndex < right.size()) ) {
if (left.get(leftIndex).compareTo(right.get(rightIndex)) <= 0) {
sorted.add(left.get(leftIndex));
leftIndex++;
} else {
sorted.add(right.get(rightIndex));
rightIndex++;
}
}
while (leftIndex < left.size()) {
sorted.add(left.get(leftIndex));
leftIndex++;
}
while (rightIndex < right.size()) {
sorted.add(right.get(rightIndex));
rightIndex++;
}
return sorted;
}
}