For a start, here is the original ArregloBinario class with the spacing fixed up:
package laboratorio9;
import java.util.Random;
import java.util.Arrays;
public class ArregloBinario
{
private int[] datos;
private static Random generador = new Random();
public ArregloBinario (int tamanio)
{
datos = new int[tamanio];
for (int i=0; i<tamanio; i++)
datos[i] = 10 + generador.nextInt(90);
Arrays.sort(datos);
}
public int busquedaBinaria(int elementoBusqueda)
{
int inferior = 0;
int superior = datos.length-1;
int medio = (inferior + superior + 1 ) / 2;
int ubicacion = -1;
// **HOW CAN I CHANGE THE FOLLOWING NTO A RECURSIVE FUNCTION>**
do
{
System.out.print(elementosRestantes(inferior,superior));
for (int i = 0; i<medio; i++)
System.out.print(" ");
System.out.println(" * ");
if (elementoBusqueda == datos[medio])
ubicacion=medio;
else if (elementoBusqueda<datos[medio])
superior = medio-1;
else
inferior = medio+1;
medio = (inferior + superior + 1) / 2;
} while ((inferior <=superior) && (ubicacion == -1));
return ubicacion;
}
public String elementosRestantes(int inferior, int superior)
{
StringBuilder temporal = new StringBuilder();
for (int i = 0; i < inferior; i++)
temporal.append( " " );
for (int i = inferior; i <= superior; i++)
temporal.append( datos[i] + " ");
temporal.append("\n");
return temporal.toString();
}
public String toString()
{
return elementosRestantes(0, datos.length-1);
}
}
And here’s a recursive version:
package laboratorio9;
import java.util.Random;
import java.util.Arrays;
public class ArregloBinario
{
private int[] datos;
private static Random generador = new Random();
public ArregloBinario (int tamanio)
{
datos = new int[tamanio];
for (int i=0; i<tamanio; i++)
datos[i] = 10 + generador.nextInt(90);
Arrays.sort(datos);
}
private int recursive (int elem, int inf, int sup, int med) {
System.out.print(elementosRestantes(inf,sup));
for (int i = 0; i<med; i++)
System.out.print(" ");
System.out.println(" * ");
if (inf > sup)
return -1;
if (elem == datos[med])
return med;
if (elem<datos[med])
return recursive (elem,inf,med-1,(inf + med) / 2);
return recursive (elem,med+1,sup,(med + sup + 2) / 2);
}
public int busquedaBinaria(int elementoBusqueda)
{
int inf = 0;
int sup = datos.length-1;
int med = (inf + sup + 1 ) / 2;
int ubi = -1;
return recursive (elementoBusqueda,inf,sup,med);
}
public String elementosRestantes(int inferior, int superior)
{
StringBuilder temporal = new StringBuilder();
for (int i = 0; i < inferior; i++)
temporal.append( " " );
for (int i = inferior; i <= superior; i++)
temporal.append( datos[i] + " ");
temporal.append("\n");
return temporal.toString();
}
public String toString()
{
return elementosRestantes(0, datos.length-1);
}
}
Here’s a sample run under Eclipse as the intensive testing it underwent 🙂
Please write the number of elements in the array.
20
14 18 19 20 22 31 43 50 55 58 58 59 62 71 72 74 85 92 95 98
Write a value (-1) to go out: 95
14 18 19 20 22 31 43 50 55 58 58 59 62 71 72 74 85 92 95 98
*
59 62 71 72 74 85 92 95 98
*
85 92 95 98
*
The value 95 was found in position 18.
Write a number (-1 to go out): -1
solved How do I do my homework with Arrays, Functions and Binary Search? [closed]