我如何创建一个循环来实现递归二进制搜索来搜索数组中的 n 个数字? JAVA

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

我的代码只打印一个数字,如何创建一个循环来搜索 n 个数字?

package binariarecursiva;

进口java.util.Scanner; /** * * @author 用户 */ 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);          
    }
}

}

你只需要稍微改变一下你的main()方法。首先,您像之前一样从用户输入中读取 n 的值。然后你循环 n 次并且在每次迭代中你询问用户他想要搜索什么样的值。

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));
    }
}

同样修改buscaBinaria()的签名,不需要n作为参数。

完整代码

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);
        }
    }
}