Calls a method that performs binary search recursively it sh
Calls a method that performs binary search recursively. it should take 4 parameters: 3 integers and an array of integers. it should return the index of the element of found, if not then it should return -1.
IN JAVA/ Eclipse
Solution
BinarySearchTest.java
import java.util.Scanner;
 public class BinarySearchTest {
    public static void main(String[] args) {
        int[] arr1 = { 1,2,3,4,5,6,7,8,9,10};
        Scanner scan = new Scanner(System.in);
       
        System.out.println(\"Enter the key: \");
        int key = scan.nextInt();
        int index = binarySearch(arr1,0,arr1.length,key);
        System.out.println(\"Index is \"+index);
       }
    public static int binarySearch(int[] array, int a, int e, int key) {
        if (a < e) {
            int mid = a + (e - a) / 2;
            if (key < array[mid]) {
                return binarySearch(array, a, mid, key);
                } else if (key > array[mid]) {
                    return binarySearch(array, mid+1, e , key);
                    } else {
                        return mid;
                    }
            }
        return -1;
    }
 }
Output:
Enter the key:
 9
 Index is 8
Enter the key:
 11
 Index is -1

