1

So I've got an assignment where I have to create a method which takes an int[] parameter and returns the smallest int. There's only one problem though...we have to use recursion! Anyway, here is my code:

static int[] arr = {12, 8, 4, 17};

public static void main(String[] args){
System.out.println("Minimum is :" + findMin(arr));
}

public static int findMin(int[] iArray){
    if(arr.length == 0) {
        System.err.println("Please Pass An Array With At Least 1 Element.");
        return (Integer) null;
    }
    else return findMinFromArray(iArray, 0, iArray[0]); //call method with starting parameters ie index = 0 & min = iArray[0]
}

private static int findMinFromArray(int[] iArray, int index, int min) {
    if(index <= (iArray.length - 1)){
        if(iArray[index] < min){
            min = iArray[index];
        }
        System.out.println("Before: " + "Index = " + index + " | Min = " + min);
        findMinFromArray(iArray, index + 1, min);
    }
    System.out.println("After: " + "Index = " + index + " | Min = " + min);
    return min;
}

And here is the output...

Before: Index = 0 | Min = 12
Before: Index = 1 | Min = 8
Before: Index = 2 | Min = 4
Before: Index = 3 | Min = 4
After: Index = 4 | Min = 4
After: Index = 3 | Min = 4
After: Index = 2 | Min = 4
After: Index = 1 | Min = 8
After: Index = 0 | Min = 12
Minimum is :12

So as you can see, the code is kind of working, but I'm not sure how to get the program to stop, rather than going back again like it's doing.

phil652
  • 1,484
  • 1
  • 23
  • 48
Josh M
  • 21
  • 2
  • 4

3 Answers3

2

Your mistake is ignoring the result of the recursive call.

Change

findMinFromArray(iArray, index + 1, min);

to

return findMinFromArray(iArray, index + 1, min);

The full fixed method :

private static int findMinFromArray(int[] iArray, int index, int min) {
    if(index <= (iArray.length - 1)){
        if(iArray[index] < min){
            min = iArray[index];
        }
        System.out.println("Before: " + "Index = " + index + " | Min = " + min);
        return findMinFromArray(iArray, index + 1, min);
    }
    System.out.println("After: " + "Index = " + index + " | Min = " + min);
    return min;
}

Output :

Before: Index = 0 | Min = 12
Before: Index = 1 | Min = 8
Before: Index = 2 | Min = 4
Before: Index = 3 | Min = 4
After: Index = 4 | Min = 4
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Yep, works perfectly thanks! Looks like I need to look over the lecture slides again though haha – Josh M Apr 24 '15 at 18:52
0

Java is pass by value. This means that if you change the value of a parameter inside the method, this change won't get reflected to the variable used when calling the method. This applies for your int min parameter.

Apart of this, you're never using the result of findMinFromArray internally to check what is the minimum value: the current value of iArray[index] or the result of this method.

I would redesign the method findMinFromArray to omit the variable int min and return the minimum value of current index and the result of this method:

private static int findMinFromArray(int[] iArray, int index) {
    if(index <= (iArray.length - 1)) {
        int current = iArray[index];
        int otherMin = findMinFromArray(iArray, index + 1);
        return (current < otherMin) ? current : otherMin;
    }
    return Integer.MAX_VALUE;
}

Not relevant to the current proble, but there's another issue here:

return (Integer) null;

This will throw a NullPointerException when you send an empty array as parameter. Instead return a value like Integer.MAX_VALUE.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

Just for fun another possible solution :

int minValue(int[] arr, int index) {
  if (index == arr.length - 1) return arr[index];
  else return Math.min(arr[index], minValue(arr, index + 1));
}

int[] arr = {12, 8, 4, 17};
System.out.println("Minimum is :" + minValue(arr, 0));

(empty array is not checked in this solution)

tomse
  • 501
  • 2
  • 7