简体   繁体   English

使用递归查找数组中的最大元素

[英]Find the maximum elements in an array using recursion

I am a algorithm beginner.I just worked out a solution to "recursively find the maximum elements in an array of integers": 我是一个算法初学者。我刚刚找到了一个解决方案,“递归地找到整数数组中的最大元素”:

public static int findMax(int[]arr)
{

  if(arr.length==1)
    return arr[0];

  return findMax(Arrays.copyOf(arr, arr.length-1))> arr[arr.length-1]?findMax(Arrays.copyOf(arr, arr.length-1)):arr[arr.length-1];
}

I did test several times, it seems to work correctly. 我做了几次测试,似乎工作正常。

However, I found someone used other recursion methods solved it too like in this post: 但是,我发现有人使用其他递归方法解决了它,就像在这篇文章中一样:

finding max value in an array using recursion java 使用递归java在数组中查找最大值

His code is like: 他的代码如下:

 int largest(int[] a, int start, int largest) 
 {
   if (start == a.length)
     return largest;
   else {
    int l = (a[start] > largest ? a[start] : largest);
    return largest(a, start + 1, l);
}

I am stuck here on the essence of difference in our thinking style for this particular problem.My thoughts is like: 对于这个特殊的问题,我的思维方式存在差异的本质。我的想法是这样的:

1.he used another parameter "start" to keep track of the current cursor to the array element in every recursion, I didn't use that since I shrink the array by 1 in every recursion and always use the tail element to compare; 1.我使用另一个参数“start”来跟踪每次递归中当前光标到数组元素的情况,我没有使用它,因为我在每次递归时将数组缩小了1并始终使用tail元素进行比较;

2.he used another parameter "largest" to keep track of the maximum value found so far. 2.他使用另一个“最大”参数来跟踪到目前为止发现的最大值。 I didn't use this in my code, but I didn't use that. 我没有在我的代码中使用它,但我没有使用它。 And this is actually where I get stuck. 这实际上是我被卡住的地方。 I didn't use that "largest" variable to keep track of the maximum in each iteration, why could I achieve the same result? 我没有使用那个“最大”变量来跟踪每次迭代的最大值,为什么我可以达到相同的结果?

Any help is greatly appreciated! 任何帮助是极大的赞赏!

First of all what nm said is right, copying array is expensive and unnecessary. 首先,nm所说的是正确的,复制阵列既昂贵又不必要。 the other algorithm use start to avoid this. 另一种算法使用start来避免这种情况。

but for your question. 但是对于你的问题。 you actually use largest just didnot name it any thing! 你实际上使用最大的只是没有命名任何东西!

return findMax(Arrays.copyOf(arr, arr.length-1))> arr[arr.length-1]?findMax(Arrays.copyOf(arr, arr.length-1)):arr[arr.length-1];

is where you find largest. 是你发现最大的地方。 it is exactly like another algorithm you mentioned. 它就像你提到的另一种算法。 first of all you find the largest in : findMax(Arrays.copyOf(arr, arr.length-1)) and then you compare its value with : arr[arr.length-1] choose whatever is larger to be you current largest. 首先,你找到最大的: findMax(Arrays.copyOf(arr, arr.length-1)) ,然后你将它的值与: arr[arr.length-1]选择你当前最大的更大值。

another advice, why you need to call findMax(Arrays.copyOf(arr, arr.length-1)) two time? 另一个建议,为什么你需要findMax(Arrays.copyOf(arr, arr.length-1))调用findMax(Arrays.copyOf(arr, arr.length-1)) simply use a variable to enhance you algorithm speed. 只需使用变量来提高算法速度。

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

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