自上而下的合并排序。合并操作不清楚
Top down mergesort. Merge operation not clear
我正在学习自上而下的合并排序,并且开始了解递归部分。我见过几个实现,其中合并是通过一系列 while 循环完成的。
然而,在下面的实现中,合并操作是不同的,它是如何工作的还不是很清楚。它似乎只是在比较索引而不是实际元素(与我见过的其他实现不同)
private void merge(int[] aux, int lo, int mid, int hi) {
for (int k = lo; k <= hi; k++) {
aux[k] = theArray[k];
}
int i = lo, j = mid+1;
for (int k = lo; k <= hi; k++) {
if (i > mid) {
theArray[k] = aux[j++];
}
else if (j > hi) {
theArray[k] = aux[i++];
}
else if (aux[j] < aux[i]) {
theArray[k] = aux[j++];
}
else {
theArray[k] = aux[i++];
}
}
}
private void sort(int[] aux, int lo, int hi) {
if (hi <= lo)
return;
int mid = lo + (hi - lo) / 2;
sort(aux, lo, mid);
sort(aux, mid + 1, hi);
merge(aux, lo, mid, hi);
}
public void sort() {
int[] aux = new int[theArray.length];
sort(aux, 0, aux.length - 1);
}
以上代码假设全局变量theArray
存在。
此 merge
方法仅使用单个循环,而不是大多数实现中使用的 3 个循环(至少我见过的大多数实现)。
前两个条件处理合并的两个源数组之一的所有元素都已添加到合并数组的情况。这些条件通常由第一个循环之后的单独循环处理,不需要比较两个源数组中的元素。
if (i > mid) { // all the elements between lo and mid were already merged
// so all that is left to do is add the remaining elements
// from aux[j] to aux[hi]
theArray[k] = aux[j++];
}
else if (j > hi) { // all the elements between mid+1 and hi were already merged
// so all that is left to do is add the remaining elements
// from aux[i] to aux[mid]
theArray[k] = aux[i++];
}
else if (aux[j] < aux[i]) { // both source arrays are not done, so you have to
// compare the current elements of both to determine
// which one should come first
theArray[k] = aux[j++];
}
else {
theArray[k] = aux[i++];
}
我正在学习自上而下的合并排序,并且开始了解递归部分。我见过几个实现,其中合并是通过一系列 while 循环完成的。
然而,在下面的实现中,合并操作是不同的,它是如何工作的还不是很清楚。它似乎只是在比较索引而不是实际元素(与我见过的其他实现不同)
private void merge(int[] aux, int lo, int mid, int hi) {
for (int k = lo; k <= hi; k++) {
aux[k] = theArray[k];
}
int i = lo, j = mid+1;
for (int k = lo; k <= hi; k++) {
if (i > mid) {
theArray[k] = aux[j++];
}
else if (j > hi) {
theArray[k] = aux[i++];
}
else if (aux[j] < aux[i]) {
theArray[k] = aux[j++];
}
else {
theArray[k] = aux[i++];
}
}
}
private void sort(int[] aux, int lo, int hi) {
if (hi <= lo)
return;
int mid = lo + (hi - lo) / 2;
sort(aux, lo, mid);
sort(aux, mid + 1, hi);
merge(aux, lo, mid, hi);
}
public void sort() {
int[] aux = new int[theArray.length];
sort(aux, 0, aux.length - 1);
}
以上代码假设全局变量theArray
存在。
此 merge
方法仅使用单个循环,而不是大多数实现中使用的 3 个循环(至少我见过的大多数实现)。
前两个条件处理合并的两个源数组之一的所有元素都已添加到合并数组的情况。这些条件通常由第一个循环之后的单独循环处理,不需要比较两个源数组中的元素。
if (i > mid) { // all the elements between lo and mid were already merged
// so all that is left to do is add the remaining elements
// from aux[j] to aux[hi]
theArray[k] = aux[j++];
}
else if (j > hi) { // all the elements between mid+1 and hi were already merged
// so all that is left to do is add the remaining elements
// from aux[i] to aux[mid]
theArray[k] = aux[i++];
}
else if (aux[j] < aux[i]) { // both source arrays are not done, so you have to
// compare the current elements of both to determine
// which one should come first
theArray[k] = aux[j++];
}
else {
theArray[k] = aux[i++];
}