简体   繁体   English

难以理解多个递归调用

[英]Difficulty understanding multiple recursive calls

function max(arr, first, last){
if (first==last) return arr[first];
mid=first+(last-first)/2;
a=max(arr, first,mid);
b=max(arr, mid+1,last);
if (a<b) return b;
return a;
}

I'm having trouble understanding this recursive function as I don't really understand how the flow works when assigning the variables. 我在理解此递归函数时遇到了麻烦,因为我不太了解分配变量时流程的工作方式。 My understanding is that when a=max(arr, first,mid); 我的理解是当a=max(arr, first,mid); a will keep calling the function until the base case occurs - so I am safe to assume while this is happening a会一直调用该函数,直到出现基本情况为止-因此我可以安全地假设发生这种情况

b=max(arr, mid+1,last);     
if (a<b) return b;
    return a;`

is not executed until 'a' is done calling? 直到“ a”完成调用才执行? And when the flow reaches 'b' and it starts recursivly calling, does it affect a=max(arr, first,mid); 当流达到“ b”并开始递归调用时,是否会影响a=max(arr, first,mid); - in that it will apply different values to a? -它将对a应用不同的值?

My understanding in context to finding the max was that a would find the maximum element in the first half and b would find the maximum element in the second half but I don't understand how it can do that when if (a<b) return b; return a; 我对找到最大值的理解是a将在上半部分找到最大元素,b将在下半部分找到最大元素,但是当if (a<b) return b; return a;时,我不知道它如何做到这一点if (a<b) return b; return a; if (a<b) return b; return a; isn't called until at the end when a and b have values, my thought was that it would check this so that it could find the maximum in a and maximum in b and then compare the max of both halves to find the maximum element in arr? 直到最后a和b都具有值时才被调用,我以为它将对其进行检查,以便它可以找到a中的最大值和b中的最大值,然后比较两个半部的最大值以找到in中的最大元素。啊?

Sorry if the Q is vague I'm just trying to better my understanding of recursion 抱歉,如果Q模糊,我只是想更好地理解递归

You're trying to understand how it works by following it step by step, chasing every single call all the way down. 您试图通过逐步跟踪,跟踪所有单个呼叫来了解其工作原理。 Don't do that, it's confusing. 不要那样做,这很令人困惑。

Look at what one call does, without worrying about what happens in functions the current invocation of max() calls. 查看一个调用的作用,而不必担心当前调用max()会在函数中发生什么。 Yes, it calls itself, and even does it twice. 是的,它会自称,甚至两次。 Both of those calls are function calls that return a value, and as far as the current invocation of max() is concerned, they might as well just be numbers. 这两个调用都是返回值的函数调用,就当前调用max()而言,它们也可能只是数字。

If I replaced the lines assigning a and b with this: 如果我将分配ab的行替换为:

a = 13;
b = 42;

then you wouldn't have any trouble understanding what it does. 这样您便可以毫无困难地了解其功能。 And if I replaced them with this: 如果我用这个替换它们:

a = max_implemented_with_a_for_loop(arr, first, mid);
b = max_implemented_with_a_for_loop(arr, mid+1, last);

then you wouldn't have trouble with that either. 那么您也不会遇到任何麻烦。 The recursive version isn't that different. 递归版本没有什么不同。

Recursive functions like this work by taking a big problem, dividing it into smaller subproblems, solving the subproblems, and combining the solutions to solve the big problem. 像这样的递归函数通过处理一个大问题,将其分为较小的子问题,解决子问题,并组合解决方案来解决该大问题。 It uses itself to solve the subproblems, and if we didn't have base cases, there would be a snag - there comes a point where you can't subdivide the problem any further, and you need another way of solving these tiny subproblems. 它使用自身来解决子问题,如果我们没有基本案例,则会遇到障碍-有时您无法进一步细分问题,并且您需要另一种解决这些小子问题的方法。 But it isn't much of a snag, since a subproblem that's too small to divide anymore is easy to solve. 但这并不是一个小问题,因为子问题太小而无法分解,因此很容易解决。 The base case just notices that we've got something easy to solve directly and/or can't be divided any further, and handles it. 基本情况只是注意到我们有容易直接解决的问题和/或无法进一步分解,并对其进行处理。

Each recursive call has a smaller non-negative value of last-first. 每个递归调用都具有较小的非负值,即“ last-first”。 So if the first call has non-negative value of last-first then each call, including the first, terminates. 因此,如果第一个呼叫的“ last-first”值为非负值,则每个呼叫(包括第一个)都将终止。

If last==first then it returns the only element in the range as maximum. 如果last == first,那么它将返回范围中的唯一元素,即最大值。

Otherwise it returns the larger of the maximum of the first half (a) and the maximum of the second half (b). 否则,它将返回上半部分的最大值(a)和下半部分的最大值(b)中的较大者。

In either case the maximum is correctly calculated, and there are no other cases. 不论哪种情况,最大值都可以正确计算,没有其他情况。

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

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