Getting the largest element in an array using recursion

I have a purpose to use recursion to get the largest element in any given array. I have the following code that will work if the largest element is not the last in the array.

Not sure how to fix this?

import java.util.Scanner; public class RecursionLargestInArray { public static void main (String[] args) { int max = -999; Scanner scan = new Scanner (System.in); System.out.print("Enter the size of the array: "); int arraySize = scan.nextInt(); int[] myArray = new int[arraySize]; System.out.print("Enter the " + arraySize + " values of the array: "); for (int i = 0; i < myArray.length; i++) myArray[i] = scan.nextInt(); for (int i = 0; i < myArray.length; i++) System.out.println(myArray[i]); System.out.println("In the array entered, the larget value is " + getLargest(myArray, max) + "."); } public static int getLargest(int[] myArray, int max) { int i = 0, j = 0, tempmax = 0; if (myArray.length == 1) { return max; } else if (max < myArray[i]) { max = myArray[i]; int[] tempArray = new int[myArray.length-1]; for (i = 1; i < myArray.length; i++) { tempArray[j] = myArray[i]; j++; } tempmax = getLargest(tempArray, max); return tempmax; } else if { int[] tempArray = new int[myArray.length-1]; for (i = 1; i < myArray.length; i++) { tempArray[j] = myArray[i]; j++; } tempmax = getLargest(tempArray, max); return tempmax; } } } 
+4
source share
4 answers

Your first condition is the problem:

 if (myArray.length == 1) { return max; } 

replace it with:

 if (myArray.length == 1) { return myArray[0] > max ? myArray[0] : max; } 

If the array has only one element, you return the previous maximum. If max is the last item, it will be skipped.

+6
source

you never evaluate the final element - you simply return max when the size of the array is 1, so you never check the last element in the array.

also a comment - instead of creating copies of the array every time, why don't you just pass the current index to your function every time you recurs?

+3
source

When the length of the array is 1, getLargest does not test the (single) element of the array for max ; it just returns max . Therefore, it always skips the last element.

As an aside, it would be better to initialize max for Integer.MIN_VALUE instead of an arbitrary value of -999.

In the future, as soon as you receive your code, it will still be terribly inefficient. You might consider sending it to codereview.stackexchange.com to get feedback.

+2
source

how to split and win?

 private static int findLargest(int lowerLimit, int upperLimit) { if (lowerLimit == upperLimit) { return temp[lowerLimit]; } else if (upperLimit - lowerLimit == 1){ return Math.max(temp[upperLimit], temp[lowerLimit]); } else { int pivot = (upperLimit - lowerLimit + 1)/2; int firstHalf = findLargest(lowerLimit, lowerLimit + pivot); int secondHalf = findLargest(upperLimit - pivot , upperLimit); return Math.max(firstHalf, secondHalf); } } 

Of course temp is a global array .. and to start the recursion you call it with findLargest (0, temp.length)

+2
source

Source: https://habr.com/ru/post/1381315/


All Articles