Java 自上而下的合并排序。合并操作不清楚

Java 自上而下的合并排序。合并操作不清楚,java,algorithm,sorting,mergesort,Java,Algorithm,Sorting,Mergesort,我正在学习自上而下的合并排序,并开始理解递归部分。我见过几个实现,其中合并是通过一系列while循环完成的 然而,在下面的实现中,合并操作是不同的,它的工作原理并不清楚。它似乎只是比较索引,而不是实际的元素(与我见过的其他实现不同) private void merge(int[]aux,int-lo,int-mid,int-hi){ 用于(int k=lo;k hi){ 阵列[k]=aux[i++]; } else if(aux[j]mid){//lo和mid之间的所有元素都已合并 //所以剩

我正在学习自上而下的合并排序,并开始理解递归部分。我见过几个实现,其中合并是通过一系列while循环完成的

然而,在下面的实现中,合并操作是不同的,它的工作原理并不清楚。它似乎只是比较索引,而不是实际的元素(与我见过的其他实现不同)

private void merge(int[]aux,int-lo,int-mid,int-hi){
用于(int k=lo;k hi){
阵列[k]=aux[i++];
}
else if(aux[j]if(hi这个
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++];
        }
if(i>mid){//lo和mid之间的所有元素都已合并
//所以剩下要做的就是添加其余的元素
//从辅助[j]到辅助[hi]
阵列[k]=aux[j++];
}
否则,如果(j>hi){//mid+1和hi之间的所有元素都已合并
//所以剩下要做的就是添加其余的元素
//从辅助[i]到辅助[mid]
阵列[k]=aux[i++];
}
否则,如果(aux[j]
您的解释很有帮助。谢谢!这不是应该是int[]aux=new int[theArray.Length];(不带-1),然后排序(aux,0,theArray.Length-1)吗?
        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++];
        }