Last active
November 6, 2022 17:14
-
-
Save luismtorresv/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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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)); | |
} | |
} | |
} |
Le metes
😸
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
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.