Good, I have created a code based on binary search, which makes recursive calls to find an Integer that is passed as a parameter (element). The problem is that when it comes to testing it (making it look for all the numbers contained in a matrix) it only finds me a few and making a triangle. (Look at image)
public boolean contiene(int[][] matriz, int elemento) {
return encontrarNumero(matriz, 0, matriz.length-1, 0, matriz[0].length-1, elemento);
}
public boolean encontrarNumero(int[][] matriz, int f0, int fN, int c0, int cN, int elem){
boolean enc= false;
if(f0==fN && c0==cN){
return (matriz[f0][cN]==elem);
}
if(f0 < fN || c0<cN){
int fk = (f0+fN)/2;
int ck = (c0+cN)/2;
if(elem > matriz[fk][ck]) {
//Busca en primer cuadrante
enc = encontrarNumero(matriz, f0, fk, c0, ck, elem);
//busca en el tercero
enc = encontrarNumero(matriz, fk+1, fN, c0, ck, elem);
}
else if (elem < matriz[fk][ck]){
//busca en el tercero
enc = encontrarNumero(matriz, fk+1, fN, c0, ck, elem);
//busca en el segundo
enc = encontrarNumero(matriz, f0, fk,ck+1, cN, elem);
//busca en el cuarto
enc = encontrarNumero(matriz,fk+1, fN, ck+1, cN, elem);
}
else if(matriz[fk][ck]==elem){
return true;
}
}
return enc;
}
The idea would be to find all the numbers inside the cardboard (as proof), but it does not and it only finds a few.
The function that calls the contains is this:
for(int i=99; i>0; i--)
if(cb.contiene(carton, i)){
if(texto.length()>0) texto.append(", ");
texto.append(i);
}
Thank you very much. Greetings