-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSorter.pde
42 lines (38 loc) · 980 Bytes
/
MergeSorter.pde
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
import java.util.LinkedList;
import java.util.Arrays;
class MergeSorter implements Sorter {
List<Instruction> plan(int[] array) {
List<Instruction> steps = new LinkedList<Instruction>();
mergeSort(steps, array, 0);
return steps;
}
void mergeSort(List<Instruction> steps, int[] array, int offset) {
if (array.length <= 1) return;
int[] a0 = Arrays.copyOfRange(array, 0, array.length/2);
int[] a1 = Arrays.copyOfRange(array, array.length/2, array.length);
mergeSort(steps, a0, offset);
mergeSort(steps, a1, offset + array.length/2);
merge(steps, a0, a1, array, offset);
}
void merge(List<Instruction> steps, int[] a0, int[] a1, int[] a, int offset) {
int i0 = 0, i1 = 0;
for (int i = 0; i < a.length; i++) {
if (i0 == a0.length) {
a[i] = a1[i1];
i1++;
}
else if (i1 == a1.length) {
a[i] = a0[i0];
i0++;
}
else if (a0[i0] < a1[i1]) {
a[i] = a0[i0];
i0++;
}
else {
a[i] = a1[i1];
i1++;
}
}
}
}