Shaghayegh Tavakoli
Shaghayegh Tavakoli

Reputation: 530

Index of the minimum value in array; "recursive"

I'm trying to find the first index of the minimum value based on the first column in a 2d array, this is my function:

n is the number of rows and k is 0 at the beginning.

public int findMinRecursive(int[][] array, int n, int k) {
    int min;
    if (n == 1) {
        return array[0][k];

    } else {
        min = findMinRecursive(array, n - 1, k);
        if(min == array[n - 1][k]){
            k++;
        }
        if (min < array[n - 1][k]) {
            return min;
        }
        else {
            return array[n - 1][k];
        }
    }
}

In case there is more than one minimum, it shows the one with the lowest value in the next column. This function works but I need to get the first index of my array too and I don't know how. This how I call the function:

int min = sortMyArray.findMinRecursive(array, n, 0);
System.out.println("Min = " + min);

Upvotes: 3

Views: 911

Answers (1)

xerx593
xerx593

Reputation: 13261

find the first index of the minimum value in a 2d array

Means (for me): In an integer Array containing a minimum element A[minI][minJ] return minI.

Now recursion can be applied on two levels (in a two-dimensional array), i recommend to recurse over the "rows" (comparing the min value of each row...and storing the index of "the row") :

Update: now with real recursion, and only comparing the first column:

public class Q50456760 {

    private static final int[][] TEST_A = {{0, 1, 2}, {3, 4, 5}, {6, 7, -1}};

    public static void main(String... args) {
        //        System.out.println(TEST_A);
        System.out.print("Min row index (expected 0): ");
        System.out.println(Q50456760.findMinRowIndexIter(TEST_A, Integer.MAX_VALUE));
        System.out.print("Recursive: ");
        System.out.println(Q50456760.findMinRowIndexRec(TEST_A, 0, Integer.MAX_VALUE, -1));
    }

    private static int findMinRowIndexIter(int[][] arr, int min) {
        int minI = -1;
        for (int i = 0; i < arr.length; i++) {
            int currMin = findMinIter(arr[i], min);
            if (currMin < min) {
                min = currMin;
                minI = i;
            }
        }
        return minI;
    }

    private static int findMinRowIndexRec(int[][] arr, int i, int min, int minI) {
        int currMin = findMinRec(arr[i], 0, min);
        if (currMin < min) {
            min = currMin;
            minI = i;
        }
        i++;
        if (i < arr.length) {
            return findMinRowIndexRec(arr, i, min, minI);
        } else {
            return minI;
        }
    }

    private static int findMinIter(int[] arr, int min) {
        int result = min;
//            for (int aj : arr) {
        if (arr[0] < min) {
            result = arr[0];
        }
//            }
        return result;
    }

    private static int findMinRec(int[] a, int j, int min) {
        if (a[j] < min) {
            min = a[j];
        }
//            j++;
//            if (j < a.length) {
//                return findMinRec(a, j, min);
//            } else {
        return min;
//            }
    }
}

Upvotes: 1

Related Questions