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

CODE :

public class Main
{
public static int BinarySearch(int arr[], int target, int low, int high)
{
if(low > high) // target not found
{
return -1;
}
int mid = (low+high)/2;
if(target == arr[mid]) // target found
{
return mid;
}
if(target < arr[mid])
{
return BinarySearch(arr, target, low, mid-1); // target lies in the left half
}
  
return BinarySearch(arr, target, mid+1, high); // target lies in the right high
}
   public static void main(String[] args) {
       int[] arr = {1,2,3,4,5};
       System.out.println("Index of 3: " + BinarySearch(arr,3,0,4));
       System.out.println("Index of 6: " + BinarySearch(arr,6,0,4));
   }
}

OUTPUT SNIPPET :

Hope this resolves your doubt, Please give an upvote if you liked my solution. Thank you :)


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
Write a linear-search Java method to test if a String array is already sorted alphabetically. The...
Write a linear-search Java method to test if a String array is already sorted alphabetically. The prototype should be static boolean ifsorted(String [] s) { }
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...
(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 −...
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...
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...
Task 2: Recursive Binary Search Part A - Target in list? Write a function simple recursive...
Task 2: Recursive Binary Search Part A - Target in list? Write a function simple recursive binary search(lst, target) which returns True if target is in lst; otherwise False. You must use recursion to solve this problem (i.e. do not use any loops). Part B - Where is target? Write a function advanced recursive binary search(lst, target, lo, hi) which returns the index of an instance of target in lst between lo and hi, or None if target is not...
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!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT