/
MergeSort.java
executable file
·43 lines (35 loc) · 1.19 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
package org.razertory.javacodelab.sort;
class MergeSort {
void sort(int[] array) {
mergeSort(array, 0, array.length - 1);
}
private void mergeSort(int[] array, int low, int high) {
if (low >= high) {
return;
}
int middle = (low + high) / 2;
mergeSort(array, low, middle);
mergeSort(array, middle + 1, high);
merge(array, low, middle, high);
}
private void merge(int[] array, int low, int middle, int high) {
// 辅助数组
int[] helper = new int[array.length];
System.arraycopy(array, 0, helper, 0, high + 1);
int helperLeft = low;
int helperRight = middle + 1;
int current = low;
while (helperLeft <= middle && helperRight <= high) {
if (helper[helperLeft] <= helper[helperRight]) {
array[current] = helper[helperLeft];
helperLeft++;
} else {
array[current] = helper[helperRight];
helperRight++;
}
current++;
}
int remaining = middle - helperLeft;
System.arraycopy(helper, helperLeft, array, current, remaining + 1);
}
}