Question

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 
// Note: for it to be recursive, we need to pass both left and right as parameters as well.
public static int binarySearchWithNulls(String[] words, String target, int left, int right) {
    if (left <= right) {
        int mid = (left + right) / 2;
        if (words[mid] == null) return -1;
        if (words[mid].equals(target)) {
            return mid;
        } else if (target.compareTo(words[mid]) < 0) {
            return binarySearchWithNulls(words, target, left, mid-1);
        } else {
            return binarySearchWithNulls(words, target, mid+1, right);
        }
    }
    return -1;
}

public static int binarySearchWithNulls(String[] words, String target) {
    if (words == null || target == null) return -1;
    return binarySearchWithNulls(words, target, 0, words.length-1);
}
Add a comment
Know the answer?
Add Answer to:
**JAVA** Write a binary search method for a String array that might contain nulls. Your method...
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
  • 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...

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

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

  • 1. What is the height of a full binary search tree that contains 63 nodes 2....

    1. What is the height of a full binary search tree that contains 63 nodes 2. What is the output of this code? Describe what this recursive code does. public class Driver {    public static void main (String[ ] args)   {        String text = “what do I do”;        System.out.println(Mystery.task(text));   } } public class Mystery {    public static int task(String exp)   {         if(exp.equals(“”)           return 0;        else           return 1 + task(exp.substring(1));    } } //from the Java 7 API documentation: public String substring(int...

  • Java, finish the recursive function that converts a binary string to decimal, do not change main...

    Java, finish the recursive function that converts a binary string to decimal, do not change main or function header. public static void main( String[] args ) { if( binToDec( "1101100" ) == 108 ) { System.out.println( "binToDec1 is correct!" ); } if( binToDec( "1011101" ) == 93 ) { System.out.println( "binToDec2 is correct!" ); } } public static int binToDec( String bin ) { }

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

  • Searching an array: --Using the template provided, create a method called search that takes an int...

    Searching an array: --Using the template provided, create a method called search that takes an int and an array as parameters. The method search should traverse the array to look for the int. search should print whether it found the int or not, for example: found: 10 did not find: 11 examples (bold face is text entered by user) % java SearchArray 20 found: 20 % java SearchArray 13 did not find: 13 % java SearchArray 100 found: 100 %...

  • Why is my java method returning "-1" even if the string is in the array? The...

    Why is my java method returning "-1" even if the string is in the array? The method is:        public static int findWord(String wordToFind {        boolean found=false;            int i=0;            while (i<words.length&& !found)                if (words[i]==wordToFind)                    found=true;                else i++;                if (found)                    return i;                else return -1;   ...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

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