public class BinarySearch {
    
    // recursively searches through the sorted array to find the target value
    public static int binarySearch(int[] arr, int start, int end, int target) {
        
        // calculate the middle index of the array
        int mid = (start + end) / 2;

        // if the middle element of the array is the target, return the index
        if (arr[mid] == target) {
            return mid;
        }
        // if the middle element is greater than the target, search the left half of the array
        else if (arr[mid] > target) {
            return binarySearch(arr, start, mid - 1, target);
        }
        // if the middle element is less than the target, search the right half of the array
        else {
            return binarySearch(arr, mid + 1, end, target);
        }
    }

    // tester
    public static void main(String[] args) {
        // initialize array and target value
        int[] arr = {1, 3, 5, 7, 9, 23, 45, 67};
        int target = 45;

        // call the binarySearch function and store the index in a variable
        int index = binarySearch(arr, 0, arr.length - 1, target);

        // print out the result
        System.out.println("Index of " + target + ": " + index);
        // expected output: "Index of 45: 6"
    }
}

// call the main function to run the program
BinarySearch.main(null);
Index of 45: 6