查找最大值在数组由递归 [英] Find maximum value in an array by recursion
问题描述
// Find a maximum element in the array.
findMax(A)
findMaxHelper(A, 0, A.length)
findMaxHelper(A, left, right)
if (left == right - 1)
return A[left]
else
max1 = findMaxHelper(A, left, (right + left) / 2)
max2 = findMaxHelper(A, (right + left) / 2, right)
if (max1 > max2)
return max1
else
return max2
我有一个很难理解正在发生的事情在这个伪code。
I am having a hard time understanding what is happening in this pseudo-code.
有人可以帮助解释正在发生的事情在每行。我需要了解这个code之前,我可以回答的问题。
Can someone help explain what is happening at each line. I need to understand this code before I can answer the questions.
我知道,函数调用findMax辅助功能findMaxHelper,然后findMaxHelper使用递归。除此之外,我真的不明白。
I know that the function findMax calls the helper function findMaxHelper, then findMaxHelper uses recursion. Other than that, I really don't understand it.
推荐答案
您正在使用分而治之算法寻找从阵列的最大元素。首先,你是除数组成单个元素(分),那么你是比较的元素(征服)。您正在使用除调用数组 findMaxHelper
递归。
You are using Divide and Conquer algorithm for finding the maximum element from the array. First you are dividing the array into individual elements(divide), then you are comparing the elements(conquer). You are dividing the array using calling findMaxHelper
recursively.
分而治之的总体思路是如图所示:
The general idea of Divide and conquer is shown in the figure:
示例:
在这里,最高
是一样的IE 离开$ C $两个参数的
findMaxHelper
功能C>和右键
。
Here max
is same as your findMaxHelper
function with two arguments i.e. left
and right
.
检查这个的例子更多的概念深入的了解。
Check this example for more in depth understanding of the concept.
这篇关于查找最大值在数组由递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!