简体   繁体   English

用递归查找数组中的最大整数

[英]Finding the maximum integer in an array with recursion

I don't even know where to start with this exercise. 我什至不知道从这个练习开始。 I have been looking at it for 2 hours without knowing what to do. 我已经看了两个小时了,不知道该怎么办。 I have been told to write a method taking an array of integers, and a range of indices (low and high). 有人告诉我写一个采用整数数组和一定范围的索引(低和高)的方法。

I have been given the hint that the base case should be when low == high , returning as a maximum the value in the range (which should be array[low] ). 我得到的提示是,基本情况应在low == high ,最大返回范围内的值(应为array[low] )。 Then, if we have more than one element in the range of the array, we can split it up, find the maximum in each piece and find the maximum of the maxima. 然后,如果我们在数组范围内有多个元素,则可以将其拆分,在每个片段中找到最大值,然后找到最大值。 However, I don't know how to proceed. 但是,我不知道如何进行。 I tried using the method by definining two new numbers, maxRange(array, low, (low+high)/2) and maxRange(array, (low+high)/2, high) , and trying to do a sort of comparison, but I'm not sure on how to go on. 我通过定义两个新数字maxRange maxRange(array, low, (low+high)/2)maxRange(array, (low+high)/2, high)尝试使用该方法,并尝试进行某种比较,但我不确定如何继续。

What you need to do is keep dividing the array in 2 halves until you are left with only one element. 您需要做的是将数组分成两半,直到只剩下一个元素。 This is the base case. 这是基本情况。

In each recursion step, call maxRange on the lower half, and maxRange on the upper half. 在每一递归步骤中,调用maxRange在下半部,并maxRange上的上半部。 These will return the maximum integer in those two ranges. 这些将返回这两个范围内的最大整数。 Now if we know the maximum integer in the lower half, and in the upper half, then clearly the biggest integer in both halves is the greater of the two. 现在,如果我们知道下半部分和上半部分的最大整数,那么显然两半中的最大整数是两者中的较大者。 Make sense? 说得通?

Another explanation: 另一种解释:

When designing recursive functions, try to pretend that you've already done it and the function works as expected. 在设计递归函数时,请尝试假装您已经完成了递归并且该函数可以按预期工作。 So, while you're still writing maxRange pretend that it already does what you want: given an array, a "low" integer (the start of the range, inclusive) and a "high" integer (the end of the range, not inclusive), it returns the biggest integer in that range. 因此,当您仍在编写maxRange假装它已经maxRange您的要求:给定一个数组,一个"low"整数(范围的开始,包括端点)和一个"high"整数(范围的结束,不是包含在内),则返回该范围内的最大整数。 Then, we just write the function maxRange that divides the problem into smaller parts for recursion until the base case. 然后,我们只需编写函数maxRange将问题分成较小的部分,以便递归直到基本情况。 When we hit the base case (low == high - 1) , we just return array[low] , since there's only one element. 当我们遇到基本情况(low == high - 1) ,我们只返回array[low] ,因为只有一个元素。 Remember, depending on the way you define your function (if high is inclusive), the base case can be either (low == high) or (low == high - 1) . 请记住,根据定义函数的方式(如果包括high ),基本情况可以是(low == high)(low == high - 1) Try it out on paper! 在纸上尝试一下! Remember that integer arithmetic returns only the integer part of a division: 3/2 is 1. 请记住,整数算术仅返回除法的整数部分:3/2为1。

What language are you using? 您使用什么语言? If you are using c# you can do... 如果您正在使用c#,则可以...

int[] arr = {1 , 2 , 3 ,4}; int [] arr = {1,2,3,4}; int max = arr.Max(); int max = arr.Max();

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

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