Jump to content

  • Log In with Google      Sign In   
  • Create Account


Array Find Largest Recursively


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Cuajin   Members   -  Reputation: 155

Like
0Likes
Like

Posted 18 April 2012 - 04:28 PM

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

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: position 6. Which is wrong.

How do I do this?

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9497

Like
0Likes
Like

Posted 18 April 2012 - 04:33 PM

I think you're confusing the array's value at a given index with the index itself.

#3 Cuajin   Members   -  Reputation: 155

Like
0Likes
Like

Posted 18 April 2012 - 04:54 PM

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.

#4 SiCrane   Moderators   -  Reputation: 9497

Like
1Likes
Like

Posted 18 April 2012 - 05:00 PM

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.

#5 ApochPiQ   Moderators   -  Reputation: 14666

Like
0Likes
Like

Posted 18 April 2012 - 05:03 PM

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.

#6 Nypyren   Crossbones+   -  Reputation: 4028

Like
0Likes
Like

Posted 18 April 2012 - 05:25 PM

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:

findIndex(myList, size-1)

to

myList[findIndex(myList, size-1)]

#7 rip-off   Moderators   -  Reputation: 8067

Like
0Likes
Like

Posted 19 April 2012 - 04:16 AM

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.

#8 Álvaro   Crossbones+   -  Reputation: 12493

Like
0Likes
Like

Posted 19 April 2012 - 05:41 AM

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).

#9 samoth   Crossbones+   -  Reputation: 4657

Like
0Likes
Like

Posted 20 April 2012 - 03:17 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS