简体   繁体   English

如何在数组递归方法中找到最小值?

[英]How find minimum in a array recursive method works?

public static int findMin(int[] numbers, int startIndex, int endIndex) {
    if (startIndex == endIndex){
        return numbers[startIndex];
    }
    else {
        int midIndex = (startIndex + endIndex) / 2;
        int leftMin = findMin(numbers, startIndex, midIndex);
        int rightMin = findMin(numbers, midIndex + 1, endIndex);
        if (leftMin < rightMin)
            return leftMin;
        else
            return rightMin;
    }
}

I really have trouble understanding this find min recursion. 我真的很难理解此查找最小递归。 This recursive method finds the minimum number in an array. 此递归方法查找数组中的最小数目。

This is how I understand it. 这就是我的理解。

Suppose I have a array 5, 3 , -5, 8, and startIndex is 0, endIndex is 3 假设我有一个数组5、3,-5、8,并且startIndex为0,endIndex为3

First time, midIndex = (0+3)/2 =1. 第一次,midIndex =(0 + 3)/ 2 = 1。 So it divided between 3 and -5. 因此,它介于3和-5之间。

And then it goes to findMin, so it passes Array, 0, 1 back to findMin. 然后转到findMin,因此将Array,0、1传递回findMin。

Then, the midIndex = (0+1)/2 = 0. And passes Array, 0, 0 back to findMin. 然后,midIndex =(0 + 1)/ 2 =0。并将Array,0,0传递回findMin。

Since startIndex 0 = endIndex 0, return numbers[startIndex](which is 5?). 由于startIndex 0 = endIndex 0,因此返回数字[startIndex](为5?)。

I can't really figure out how this method finds the minimum number. 我真的不知道这种方法是如何找到最小数量的。 Since the startIndex is alway 0, why does it need to return numbers[startIndex]? 由于startIndex始终为0,为什么它需要返回数字[startIndex]?

This is the general idea that the code is implementing: 这是代码正在实现的一般想法:

To find the minimum element of an array, we can find the minimum of each half of the array and then take the minimum of those two numbers. 要找到数组的最小元素,我们可以找到数组每半的最小值,然后取这两个数字中的最小值。

How do we find the minimum of each half? 我们如何找到每一半的最小值? We simply use the same technique of breaking that up into quarters. 我们只是使用相同的技术将其分解为四分之一。

Eventually we'll be asking ourselves what the min element of a single element is, which is of course that element. 最终,我们将问自己一个元素的min元素是什么,当然是那个元素。

The algorithm shown in the image implements this recipe. 图像中显示的算法实现了该配方。

Your function is basically splitting your array in half to make the search each time. 您的功能基本上是将数组分成两半,以便每次都进行搜索。

Let's go step by step into a very simple input: 让我们逐步进入一个非常简单的输入中:

  • array: [5, 3] 数组:[5,3]
  • startIndex: 0 startIndex:0
  • endIndex: 1 endIndex:1

The first call is going to be 第一个电话是

  • findMin([5,3], 0, 1) -> (we are here) findMin([5,3],0,1)-> (我们在这里)

Since startIndex and endIndex are not the same, the array will be split into two parts: [5] and [3] , respectively. 由于startIndexendIndex不同,因此该数组将分为两部分:分别为[5][3] Although there is only one value inside each array is still an array, remember that. 尽管每个数组中只有一个值仍然是数组,但请记住这一点。 First, the leftMin variable is resolved, so the stack will look like this: 首先,解决了leftMin变量,因此堆栈如下所示:

  • findMin([5,3], 0, 1) -> (pending result) findMin([5,3],0,1)->(待定结果)
  • leftMin = findMin([5], 0, 0) -> (we are here) leftMin = findMin([5],0,0)-> (我们在这里)

As we can see both startIndex and endIndex are the same, so returns our number. 如我们所见, startIndexendIndex相同,因此返回我们的数字。 Next step is to verify the rightMin . 下一步是验证rightMin Let's update our stack: 让我们更新堆栈:

  • findMin([5,3], 0, 1) -> (pending result) findMin([5,3],0,1)->(待定结果)
    • leftMin = 5 leftMin = 5
    • rightMin = findMin([3], 1, 1) -> (we are here) rightMin = findMin([3],1,1)-> (我们在这里)

The same thing that happened to leftMin happens here: we have the same index value, so the number is returned in the first if condition. 在这里发生与leftMin相同的事情:我们具有相同的索引值,因此该数字在第一个if条件中返回。 Now the stack looks like this: 现在堆栈看起来像这样:

  • findMin([5,3], 0, 1) -> (we are here) findMin([5,3],0,1)-> (我们在这里)
    • leftMin = 5 leftMin = 5
    • rightMin = 3 rightMin = 3

Now we went back to the top of our stack and we have the values for leftMin and rightMin of our firstCall. 现在,我们回到堆栈的顶部,并具有leftMinrightMin的值。 rightMin is going to be returned, since it holds the minimum value between them. rightMin将被返回,因为它持有它们之间的最小值。

Event though we just when to the second level of recursion, the same logic applies to each new level: the method is going to split the array in half until it has only one value and them it will return this value. 尽管我们只是何时到达第二级递归,但每个新级别都适用相同的逻辑:该方法将把数组拆分成两半,直到只有一个值,然后它们将返回该值。 After that, both the left and the right value will be compared, and each level will return the min value, until there the very first one, that will define the min value for the entire array. 之后,将比较左值和右值,每个级别将返回最小值,直到第一个值定义整个数组的最小值。

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

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