java归并排序
private void mergeSort(int[] array, int begin, int end) {
if (begin >= end || begin < 0 || end > array.length - 1) {
return;
}
int mid = begin + (end - begin) / 2;
mergeSort(array, begin, mid);
mergeSort(array, mid + 1, end);
merge(array, begin, mid, end);
}
private void merge(int[] array, int begin, int mid, int end) {
int b1 = begin;
int b2 = mid + 1;
int length = end - b1 + 1;
int[] tmp = new int[length];
int index;
for (index = 0; index < length && b1 <= mid && b2 <= end; index++) {
if (array[b1] > array[b2]) {
tmp[index] = array[b2];
b2++;
} else {
tmp[index] = array[b1];
b1++;
}
}
if (b1 > mid) {
for (; index < length; index++) {
tmp[index] = array[b2];
b2++;
}
} else {
for (; index < length; index++) {
tmp[index] = array[b1];
b1++;
}
}
System.arraycopy(tmp, 0, array, begin, length);
}