简体   繁体   中英

How do i create a loop that implement a recursive binary search to search for n numbers in a array? JAVA

My code only print one number, how can i create a loop to search n numbers?

package binariarecursiva;

import java.util.Scanner; /** * * @author User */ public class BinariaRecursiva {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) 
{

  int[] array = {1, 3, 5, 6, 8, 12, 19, 21, 27, 31, 35, 45, 80, 81, 82, 85, 87, 89, 95, 101, 200, 501, 707, 1000};
  Scanner teclado = new Scanner(System.in);
  int n = teclado.nextInt();
  int x = teclado.nextInt();
  int esquerda = 0;
  int direita = array.length-1;
  System.out.println(buscaBinaria(array,  esquerda,  direita, x, n));
}

public static int buscaBinaria (int[] array, int esquerda, int direita, int x, int n)
{
    int meio = (esquerda + direita)/2; 
    if(direita < esquerda)
    {
        return -1; 
    }

    if(x==array[meio])
    {
        return meio; 
    }
    else if(x<array[meio])
    {
       return buscaBinaria(array, esquerda, meio - 1, x);
    }
    else
    {
        return buscaBinaria(array, meio + 1, direita, x);          
    }
}

}

You just need to change your main() method a bit. First you read the value for n from user input as you already did. Then you loop n times and on each iteration you ask the user what kind of value he would like to search for.

public static void main(String[] args)
{
    int[] array = {1, 3, 5, 6, 8, 12, 19, 21, 27, 31, 35, 45, 80, 81, 82, 85, 87, 89, 95, 101, 200, 501, 707, 1000};
    Scanner teclado = new Scanner(System.in);
    System.out.print("Enter number of runs: ");
    int n = teclado.nextInt();
    int esquerda = 0;
    int direita = array.length-1;
    for(int i = 0; i < n; i++) {
        System.out.print("Enter number to search for: ");
        int x = teclado.nextInt();
        System.out.println(buscaBinaria(array, esquerda, direita, x));
    }
}

Also change the signature of buscaBinaria() , it does not need the n as a parameter.

Full code

package binariarecursiva;
import java.util.Scanner;

public class BinariaRecursiva {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {

        int[] array = {1, 3, 5, 6, 8, 12, 19, 21, 27, 31, 35, 45, 80, 81, 82, 85, 87, 89, 95, 101, 200, 501, 707, 1000};
        Scanner teclado = new Scanner(System.in);
        System.out.print("Enter number of runs: ");
        int n = teclado.nextInt();
        int esquerda = 0;
        int direita = array.length-1;
        for(int i = 0; i < n; i++) {
            System.out.print("Enter number to search for: ");
            int x = teclado.nextInt();
            System.out.println(buscaBinaria(array, esquerda, direita, x));
        }
    }

    public static int buscaBinaria (int[] array, int esquerda, int direita, int x)
    {
        int meio = (esquerda + direita)/2;
        if(direita < esquerda)
        {
            return -1;
        }

        if(x==array[meio])
        {
            return meio;
        }
        else if(x<array[meio])
        {
            return buscaBinaria(array, esquerda, meio - 1, x);
        }
        else
        {
            return buscaBinaria(array, meio + 1, direita, x);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM