简体   繁体   English

递归自顶向下合并排序

[英]Recursive top down mergesort

I'm trying to get my head round the recursive sort function that's part of the mergesort algorithm. 我试图绕过合并排序算法一部分的递归排序函数。 Here's the code I have, which I'm almost certain is correct (following an online course). 这是我的代码,我几乎可以肯定是正确的(在线课程之后)。

    private static void sort(Comparable[] a, Comparable[] aux, int low, int high) {
         if (high <= low) return;
         int mid = low + (high - low) / 2;
         sort (a, aux, low, mid);
         sort (a, aux, mid+1, high);
         merge(a, aux, low, mid, high);
   }

I understand what mergesort does- it breaks each subarray down into 2 smaller subarrays, repeating this until the subarrays are of length one (and by definition sorted), and then merges. 我知道mergesort的作用-将每个子数组分解为2个较小的子数组,重复此操作直到子数组的长度为1(并按定义排序),然后合并。 However, the actual METHOD that this sort function uses to accomplish this is hard for me to follow. 但是,这种排序函数用来完成此操作的实际方法对我来说很难。 Maybe because I'm not used to recursive functions, but I was wondering if anyone can illuminate the order of operations and what the arguments are when the first merge occurs. 也许是因为我不习惯使用递归函数,但是我想知道是否有人可以阐明操作的顺序以及第一次合并时的参数是什么。

For example, when it hits the FIRST recursive call to sort(a, aux, low, mid) - does it break operation and not proceed to the second sort and the merge functions below, instead immediately calling sort again with the new parameters? 例如,当它到达对sort(a,aux,low,mid)的FIRST递归调用时,它是否中断操作并且不继续进行第二个排序和下面的合并功能,而是立即使用新参数再次调用sort? When, in this case, would the second call to sort; 在这种情况下,何时调用第二次排序; sort(a, aux, mid+1, high); 排序(a,aux,mid + 1,high); occur? 发生?

Thanks for any help. 谢谢你的帮助。 This is the first time I've posted here, if there are any formatting issues please let me know. 这是我第一次在这里发布,如果有任何格式问题,请告诉我。

When it hits the FIRST recursive call to sort(a, aux, low, mid) - does it break operation and not proceed to the second sort and the merge functions below, instead immediately calling sort again with the new parameters? 当它到达对sort(a,aux,low,mid)的FIRST递归调用时,它会中断操作,而不继续进行第二个排序和下面的合并功能,而是立即使用新参数再次调用sort吗?

  • Yes. 是。 it calls the function again ( Recursion ) for the new parameters, go down until it sorts that part. 它会再次为新参数调用函数( Recursion ),然后向下进行排序,直到对该部分进行排序。

When, in this case, would the second call to sort; 在这种情况下,何时调用第二次排序; sort(a, aux, mid+1, high); 排序(a,aux,mid + 1,high); occur? 发生?

  • After the first call is done executing. 在第一个调用完成后执行。 (When the first part is finished sorting). (当第一部分完成排序时)。

Comments explaining each step in case you didn't get any: 注释说明每个步骤,以防万一您一无所获:

 if (high <= low) return; // if your high and low numbers are the same then this part of the loop is finished and it will start going back up
 int mid = low + (high - low) / 2;//sets mid
 sort (a, aux, low, mid);//recursively calls itself so that it will eventually hit the first if. This handles from low to mid
 sort (a, aux, mid+1, high); //this handles from mid+1 to high
 merge(a, aux, low, mid, high); //This is where the merge starts. 

I find it helps to run a simple example and run through it over pen and paper if you're really having a hard time. 我发现运行一个简单的示例并在笔和纸上遍历整个示例非常有帮助,如果您真的很难的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM