# Array Find Largest Recursively

This topic is 2106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I understand recursion is not good for this sort of operation, but I am trying to learn.

[color=#008000]I want to find the position of the largest int in an array recursively.

myList = {5, 2, 3 , 1, 22, 4, 9} ;

 public int findIndex(int[] myList, int size) { if (size == 1) return size-1; return myList[size-1] > findIndex(myList, size-1) ? size-1 : size ; } 

My code produces this output: [color=#FF0000]position 6. [color=#000000]Which is wrong.

[color=#000000]How do I do this?

##### Share on other sites
I think you're confusing the array's value at a given index with the index itself.

##### Share on other sites
I believe I need to check the values at a different position. Then return the larger of the two but as a position instead of a number. I am confused though.

##### Share on other sites
Let me be more explicit. Here:
 myList[size-1] > findIndex(myList, size-1) 
You're comparing a value in the list against the return value of findIndex(), which is an index.

##### Share on other sites
This is the algorithm you probably want:

Given a start index and end index (end = one past the last element in a range)
- If range is one element long (i.e. end = start + 1), return start index
- LargestIndex = Recursively call the function with start+1, end
- Compare the value at LargestIndex with the value at start
- If start is larger, return start; else return LargestIndex

That should work, I think.

##### Share on other sites
General layout of the algorithm looks fine to me, it's identical to a typical functional-language linked list recursion (with the "head" at the last slot, meaning you only need one variable to keep track of position, since 0 will be the end)

I think you just need to change:

[color="#000088"][color="#000000"]findIndex[color="#666600"]([color="#000000"]myList[color="#666600"],[color="#000000"] size[color="#666600"]-[color="#006666"]1[color="#666600"])

to

[color="#000088"]myList[[color="#000000"]findIndex[color="#666600"]([color="#000000"]myList[color="#666600"],[color="#000000"] size[color="#666600"]-[color="#006666"]1[color="#666600"])]

##### Share on other sites
During each call, you return either size or size-1. Thus, it is not possible for your implementation to return an early index in the array - regardless of the recursion.

When designing such a generic algorithm, you should think about corner cases. For example, what should the function do if passed an empty list? An "easy" answer is just to reject such a case using an exception.

A stylistic note is that the name of the function is ambiguous, and the parameter name "size" is ambiguous (as Java arrays know their size, it appears redundant). Most programmers are used to the convention that searches start from the beginning. Thus, I would propose a function signature(s) like the following:
 public class Example { public static int indexOfLargest(int [] array, int startIndex) { // ... } public static int indexOfLargest(int [] array) { return indexOfLargest(array, 0); } } 
The most general function is indexOfLargest(int [] array, int begin, int end), if you really want maximum flexibility.

##### Share on other sites
The version that takes begin and end also allows for a different algorithm, where you divide the incoming data into two parts and recursively compute the maximum of each one. This only uses O(log(N)) stack space as opposed O(N).

##### Share on other sites
This won't teach you about recursion, but why not use a priority queue, which is the right tool for the task? Learning to use the right tool is important to learn as well.
Or, sort an array of indices (recursively if you want), which lets you find the largest and next largest element in the original array by iterating over it linearly.