Question: 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.

 I really have trouble understanding this find min recursion. This recursive

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]? Can you please write step by step how this method works? Thank you.

public static int findin (int numbers int startIndex, int end Index) f if (startIndex end Index) return numbers[startIndex else int mid Index (startIndex end Index) 2 int leftMin findMin (numbers start Index mid Index) int rightMin findMin (numbers midIndex 1, end Index) if (leftMin rightMin) return leftMin; else return rightMin

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!