在下面的代码中,每次在合并排序方法中的条件内都会调用合并if (low < high) {
?我认为它每次都被称为递归合并排序方法的一部分?
package sorting;
public class MyMergeSort extends Print{
private static int[] numbers;
private static int[] helper;
private static int number;
public static void main(String args[]){
int[] array = {1 , 3 , 5 , 7 , 8};
sort(array);
}
public static void sort(int[] values) {
numbers = values;
number = values.length;
helper = new int[number];
mergesort(0, number - 1);
}
private static void mergesort(int low, int high) {
// Check if low is smaller then high, if not then the array is sorted
if (low < high) {
// Get the index of the element which is in the middle
int middle = low + (high - low) / 2;
println("low is "+low); println("high is "+high); println("middle is "+middle);
// Sort the left side of the array
mergesort(low, middle);
// Sort the right side of the array
mergesort(middle + 1, high);
// Combine them both
merge(low, middle, high);
}
}
private static void merge(int low, int middle, int high) {
// Copy both parts into the helper array
for (int i = low; i <= high; i++) {
helper[i] = numbers[i];
}
int i = low;
int j = middle + 1;
int k = low;
// Copy the smallest values from either the left or the right side back
// to the original array
while (i <= middle && j <= high) {
if (helper[i] <= helper[j]) {
numbers[k] = helper[i];
i++;
} else {
numbers[k] = helper[j];
j++;
}
k++;
}
// Copy the rest of the left side of the array into the target array
while (i <= middle) {
numbers[k] = helper[i];
k++;
i++;
}
}
}