Question

In: Computer Science

Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...

Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be

public int BinarySearch(int target, int low, int high)

Solutions

Expert Solution

Inorder to perform binary search on a sorted integer array , we need to have an array in the program which needed to be passed to the function. But in the question it is mentioned that the siganture of methos could be :

public int BinarySearch(int target, int low, int high). Since an array also neede to be passed to the method , to perform the operation, my take on this would be that the signature of the function should be like :

public int BinarySearch(int a[] , int target, int low, int high) where "a[]" will be the integer array we'll be using.

Since it is a sorted array, I haven't coded the program asking for user input for elements of the array but the I have declared and initilaised the array inside the program itself as well as the target , which is the element to be searched for. You can always change the code to a user input one , if you wish to do so.

PROGRAM CODE

import java.util.*;
class Main{  
    //recursive method for binary search
    public static int BinarySearch(int a[], int target, int low, int high){  
        //if array is in order then perform binary search on the array
        if (high>=low){  
            //calculate mid
            int mid = low + (high - low)/2;  

            //if target =a[mid] return mid
            if (a[mid] == target){  
            return mid;  
            }  

            //if a[mid] > target then target is in left half of array
            if (a[mid] > target){  
            return BinarySearch(a, target, low, mid-1);//recursively search for target  
            }else       //target is in right half of the array

            {  
            return BinarySearch(a, target, mid+1, high);//recursively search for target 
            }  
        }  
        return -1;  
    }
        public static void main()
        {  
        //define array and key i,e,target
        int a[] = {1,11,21,31,41,51,61,71,81,91}; 
        System.out.println("Input List: " + Arrays.toString(a));
        int target = 10;  
        System.out.println("\nThe key to be searched:" + target);
        int high=a.length-1;
        //call BinarySearch method 
        int c = BinarySearch(a,target,0,high);  
        //print the result
        if (c == -1)  
            System.out.println("\nKey not found in given list!");  
        else 
            System.out.println("\nKey is found at location: "+ c + " in the list");  
    }  
}

OUTPUT

I have checked the output for two cases as shown below :


Related Solutions

Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of comparable objects L, and a new item x, find the location (index) of x or indicated that x is not in L. 5a) Give a recursive algorithm for binary search. No, while, for, repeat etc statements are allowed. Only if, if else and assignment statements are allowed. 5b) Write a difference equation for the running time of your Binary Search algorithm. Solve your equation...
Java String search Design and implement a recursive version of a binary search.  Instead of using a...
Java String search Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are...
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array of size n into k subsets each of size n/k and recursively searches only one of these k subsets. Which one of these k subsets should be searched is decided by making (k − 1) comparisons, each of which can be done in constant time. Clearly, the recurrence relation for this k-ary search algorithm can be written as, T(n) = T(n/k) + (k −...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers.
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers. Your program should have a main routine that does the following:(a) Prompt the user to enter all the 10 integers in the array.(b) Prompt the user to enter the number to be searched.(c) Reads the integer values and makes sure it is a positive integer.(d) Prints the index of the integer. If the input is not available in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT