Question

(Recursive Binary Search) Write a recursive method recursiveBinarySearch to perform a binary search of an array....

(Recursive Binary Search) Write a recursive method recursiveBinarySearch to perform a binary search of an array. The method should receive the search key, starting index and ending index as arguments. If the search key is found, return its index in the array. If the search key is not found, return –1. (NOTE: Complete the recursiveBinarySearch method in the BinaryArray class). java

0 0
Add a comment Improve this question Transcribed image text
Answer #1


public class Main
{
static int recursiveBinarySearch(int arr[], int l, int r, int x)
{
       //inputs
       //arr:array
       //x:search key
       //l:starting index
       //r:ending index
if (r >= l)
       {
int mid = l + (r - l) / 2; //finding mid value
  
if (arr[mid] == x) //matching with x
return mid; //if matches then returning index
  
if (arr[mid] > x) //if less than current mid value
return recursiveBinarySearch(arr, l, mid - 1, x);//then selecting left subarray
  
return recursiveBinarySearch(arr, mid + 1, r, x); //selecting right subarray
}

return -1;
}
  
   public static void main(String[] args) {
   //testing above function
   int arr[] = { 1,2,2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = recursiveBinarySearch(arr, 0, n - 1, x);
       if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index " + result);
   }
}

output:

Element found at index 5


Add a comment
Know the answer?
Add Answer to:
(Recursive Binary Search) Write a recursive method recursiveBinarySearch to perform a binary search of an array....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 6 (10 points Remember the recursive Searching algorithm Binary Search. Write a recursive method to search...

    6 (10 points Remember the recursive Searching algorithm Binary Search. Write a recursive method to search for a target character in the array and return the index location if found or -1 if it is not found. 7 a5 points 17 points cach) Write the code to create a GUI based class Temperature Converter which inherits from JFrame and implements the ActionListerner interface. public static int binary Search(char target, char( theValues, int firstIndex, int lastindex) Example: Clicked "F to C"...

  • Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application...

    Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...

  • **JAVA** Write a binary search method for a String array that might contain nulls. Your method...

    **JAVA** Write a binary search method for a String array that might contain nulls. Your method should not crash or throw a runtime exception. See the driver program for examples. The method header is: public static int binarySearchWithNulls(String[] words, String target) must be recursive.

  • When binary searching a sorted array that contains more than one key equal to the search...

    When binary searching a sorted array that contains more than one key equal to the search key, the client may want to know the index of either the first or the last such key. Accordingly, implement the following API: public class BinarySearchDeluxe { /* Returns the index of the first key in a[] that equals the search key, or -1 if no such key. */ public static int firstIndexOf(Key[] a, Key key, Comparator comparator) /* Returns the index of the...

  • Write a recursive function that returns the minimum key value in a binary search tree of...

    Write a recursive function that returns the minimum key value in a binary search tree of distinct (positive) integers. Return -1 in case the tree is empty. (b) Write a recursive function that returns the predecessor of the key value k in a binary search tree of distinct (positive) integers. This is the key value that precedes k in an inorder traversal of the tree. If k does not exist, or if k has no predecessor, your function should return...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an...

    Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an array that have ten integer values, user can enter the values or assigned the values to the array in the main method. The search key should be a value in the array or not in the array. JAVA!!!

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

  • Complete the binary search function. def binary Search (array, first, last, key): if last >= first:...

    Complete the binary search function. def binary Search (array, first, last, key): if last >= first: mid = int(first + (last - first)/2) if array[mid] return mid key: elif array[mid] > key: ##Statement 1 else: ##Statement 2 else: return -1 Statement 1: return binarySearch(array, last, mid+1, key) Statement 2: return binarySearch(array, mid-1, first, key) Statement 1: return binarySearch(array, first, mid+1, key) Statement 2: return binarySearch(array, mid-1, last, key) Statement 1: return binarySearch(array, mid-1, first, key) Statement 2: return binary Search(array,...

  • Write A JAVA mergesort method that mergesorts array elements in descending order. Write A JAVA binary...

    Write A JAVA mergesort method that mergesorts array elements in descending order. Write A JAVA binary search method that searches x in a sorted array with n elements and returns its position if exist, -1 otherwise.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT