Skip to content

Instantly share code, notes, and snippets.

@ludwigtuerme
Last active November 6, 2022 17:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ludwigtuerme/18e636c69c4f57910b6cceca577393b8 to your computer and use it in GitHub Desktop.
Save ludwigtuerme/18e636c69c4f57910b6cceca577393b8 to your computer and use it in GitHub Desktop.
Recursively find smallest integer in 2D array (Java). Disclaimer: This program does not really make sense.
/**
* Find the smallest integer in a 2D array.
* This program does not really make sense.
*
* @author Luis M. Torres-Villegas
* @version 2022.11.05
*/
public class RecursiveSmallestInteger2DArray {
public static void main(String args[]) {
// Tests:
// int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // 1
// int[][] arr = { { 67, 68, 69 }, { 5, 4, 76 }, { 20, 3, 16 } }; // 3
int[][] arr = { { 100, 102, 103 }, { 99, 95, 76 }, { 85, 77, 92 } }; // 76
int result = searchForSmallestInteger(arr, 0);
System.out.println(result);
}
public static int searchForSmallestInteger(int[][] arr, int i) {
if (i == arr.length - 1) {
return searchForSmallestInteger(arr[i], 0);
} else {
return Math.min(searchForSmallestInteger(arr[i], i), searchForSmallestInteger(arr, i + 1));
}
}
public static int searchForSmallestInteger(int[] arr, int i) {
if (i == arr.length - 1) {
return arr[i];
} else {
return Math.min(arr[i], searchForSmallestInteger(arr, i + 1));
}
}
}
@ludwigtuerme
Copy link
Author

En la plataforma de @eafit, creo que falta pasarle un int i que sirva como índice para indicar el siguiente.

Copiar el arreglo a partir del índice 1 es una alternativa.

@joseduquep
Copy link

Le metes

@ludwigtuerme
Copy link
Author

😸

@ludwigtuerme
Copy link
Author

Creí que se debía solucionar recursivamente…

¡Quién los manda a juntar dos talleres en uno!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment