Question

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 Greturn index where i tem is found or NOT FOUND if not found 6 public static <AnyType extends Comparable? super AnyType 7 int binary Search AnyType I J a, AnyType x return binary Search a, x, 0, a.length -1 10 11 12 Hidden recursive routine. 13 14 15 private static KAnyType extends Comparable super AnyType» 16 int binarySearchC Any Type C a, AnyType x, int low int high 17 if( low high 18 return NOT FOUND 19 20 int mid- low high 2; 21 22 if( a[ mid J.compareTo( x 0 return binarySearch( a x, mid 1, high else if( a[ mid J.compareTo( x 0 25 return bina rySearch( a, x, low, mid 1 26 else 27 return mid 28 29

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

Hi, Please find my implemetation.

I have also prited trace of execution.

public class BinarySearch {

   public static <AnyType extends Comparable<? super AnyType>>

   int binarySearch(AnyType[] a, AnyType target) {

       return binarySearch(a, target, 0, a.length - 1);

   }

   public static <AnyType extends Comparable<? super AnyType>>

   int binarySearch(AnyType[] a, AnyType target, int min, int max) {

       if (min > max) {

           System.out.println("Min: "+min+", Max: "+max);

           return -1; // target not found

       } else {

           int mid = (min + max) / 2;

           System.out.println("Min: "+min+", Mid: "+mid+", Max: "+max);

           if (a[mid].compareTo(target) < 0) { // too small; go right

               return binarySearch(a, target, mid + 1, max);

           } else if (a[mid].compareTo(target) > 0) { // too large; go left

               return binarySearch(a, target, min, mid - 1);

           } else {

               return mid; // target found; a[mid] == target

           }

       }

   }

   public static void main(String[] args) {

       Integer[] list = {-2, 8, 13, 22, 25, 25, 38, 42, 51, 103};

       System.out.println("Returned Value: "+binarySearch(list, 103, 0, 9));

       System.out.println();

       System.out.println("Returned Value: "+binarySearch(list, 30, 2, 8));

   }

}

/*

Sample run:

Min: 0, Mid: 4, Max: 9

Min: 5, Mid: 7, Max: 9

Min: 8, Mid: 8, Max: 9

Min: 9, Mid: 9, Max: 9

Returned Value: 9

Min: 2, Mid: 5, Max: 8

Min: 6, Mid: 7, Max: 8

Min: 6, Mid: 6, Max: 6

Min: 6, Max: 5

Returned Value: -1

*/

Add a comment
Know the answer?
Add Answer to:
Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application...
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
  • #2Trace These JAVA searches by hand on the following dataset. Sorted Data Set: 3, 5, 8,...

    #2Trace These JAVA searches by hand on the following dataset. Sorted Data Set: 3, 5, 8, 9, 12, 14, 18, 19, 21, 23, 24, 26 #Trace the sorted data set using a binary search. Search target: 23 #Trace the sorted data set using a binary search. Search target: 15 (To trace a binary search, list the value of first, last, and mid for each pass through the method.) Use this method: private static <T extends Comparable<? super T>>    boolean...

  • Java The following questions ask about tracing a binary search. To trace the binary search, list...

    Java The following questions ask about tracing a binary search. To trace the binary search, list the value of first, last, and mid for each pass through the loop or method. Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) { boolean found = false; int first = 0; int last = numbers.length - 1; while (first <= last && !found) { int mid = (first + last) / 2; if (numbers[mid] == target)...

  • (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

  • 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"...

  • JAVA Write a program which will read a text file into an ArrayList of Strings. Note...

    JAVA Write a program which will read a text file into an ArrayList of Strings. Note that the given data file (i.e., “sortedStrings.txt”) contains the words already sorted for your convenience. • Read a search key word (i.e., string) from the keyboard and use sequential and binary searches to check to see if the string is present as the instance of ArraryList. • Refer to “SearchInt.java” (given in “SearchString.zip”) and the following UML diagram for the details of required program...

  • Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch...

    Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...

  • Qi. Create a java project Question that includes: • A bubble sort method to sort your...

    Qi. Create a java project Question that includes: • A bubble sort method to sort your arrays. Implement the below recursive binary search. • A java main class where you: o Create an array of characters that contains the letters of your first name followed by your last name, without spaces. o Call the sorting method to sort the array o Call binary search method to find the position of the first letter 'a' in your array. // Recursive Binary...

  • **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.

  • Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary...

    Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary search algorithm on the following array of characters: A E G K M O R S Z. For each iteration of binary search use a table similar to the table below to list: (a) the left index and (b) the right index of the array that denote the region of the array that is still being searched, (c) the middle point of the array,...

  • Given the binary search function, answer following question: int binarySearch(int a[], int size, int target, int...

    Given the binary search function, answer following question: int binarySearch(int a[], int size, int target, int low, int high) { while (low <= high) { int mid = (low + high) / 2; if (a[mid] == target) return mid; else if (a[mid] < target) low = mid + 1; else high = mid 1: } return -1; } 1) If array a[] = {4, 5, 18, 25, 66, 70, 78}, size = 7, target = 71, low = 0, high...

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