• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 635
  • Last Modified:

How do I make a recursive method for finding the largest element in an array A of N elements?

I need to know how to start a program that uses a recursive method in order to find the largest element in an array A of N elements.
0
15jen
Asked:
15jen
2 Solutions
 
objectsCommented:
try something like:

public int largest(int[] array)
{
   return largest(array, 0);
}

int largest(int[] array, int start)
   if (start==array.length-1) return array[start];
   else return Math.max(array[start], largest(array, start+1);
}

0
 
Ajay-SinghCommented:
You can use something like this:

int max(int[] A) {
      return max(A, 0, -Integer.MAX_VALUE);
}

int max(int[] A, int start, int max) {
      if(start>=A.legnth) {
            return max;
      }
      if(A[i]>max) {
            return max(A, start + 1, A[i]);
      }
      return max(A, start + 1, max);
}
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now