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