0
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

First time, midIndex = (0+3)/2 =1. So it divided between 3 and -5.

And then it goes to findMin, so it passes Array, 0, 1 back to findMin.

Then, the midIndex = (0+1)/2 = 0. And passes Array, 0, 0 back to findMin.

Since startIndex 0 = endIndex 0, return numbers[startIndex](which is 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]?

0

2 Answers 2

1

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.

The algorithm shown in the image implements this recipe.

Sign up to request clarification or add additional context in comments.

Comments

0

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]
  • startIndex: 0
  • endIndex: 1

The first call is going to be

  • findMin([5,3], 0, 1) -> (we are here)

Since startIndex and endIndex are not the same, the array will be split into two parts: [5] and [3], respectively. 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:

  • findMin([5,3], 0, 1) -> (pending result)
  • leftMin = findMin([5], 0, 0) -> (we are here)

As we can see both startIndex and endIndex are the same, so returns our number. Next step is to verify the rightMin. Let's update our stack:

  • findMin([5,3], 0, 1) -> (pending result)
    • leftMin = 5
    • rightMin = findMin([3], 1, 1) -> (we are here)

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. Now the stack looks like this:

  • findMin([5,3], 0, 1) -> (we are here)
    • leftMin = 5
    • rightMin = 3

Now we went back to the top of our stack and we have the values for leftMin and rightMin of our firstCall. rightMin is going to be returned, since it holds the minimum value between them.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.