Question

In: Computer Science

binarySearchLengths(String[] inArray, String search, int start, int end) This method will take in a array of...

binarySearchLengths(String[] inArray, String search, int start, int end)

This method will take in a array of strings that have been sorted in order of increasing length, for ties in the string lengths they will be in alphabetical order. The method also takes in a search string and range of indexes (start, end). The method will return a String formatted with the path of search ranges followed by a decision (true or false), see the examples below.

Example 1: binarySearchLengths({"a","aaa","aaaaa"},"bb",0,2) would return the string "(0,2) (0,0) False" This means the method first looked in the range 0 to 2, then made a recursive call to the range 0 to 0, then made the determiniation that the element was not in the array "False".

Example 2: binarySearchLengths({"a","b","c","d","e","f","g"},"e",0,6) would return the string "(0,6) (4,6) (4,4) True" This means the method first looked in the range 0 to 6, then made a recursive call to the range 4 to 6, then made a recursive call to the range 4 to 4, then made the determiniation that the element was in the array "True".

Solutions

Expert Solution

Java code

public class T{

    public static String binarySearchLengths(String[] inArray, String search, int start, int end){

        String Answer = "";

        boolean found = false;

        while(start <= end){

            Answer +="("+ Integer.toString(start) +","+ Integer.toString(end) + ") ";

            int mid = (start + end)/2;

            if(inArray[mid].compareTo(search) == 0){

                found = true;

                break;

            }

            else if(inArray[mid].length() > search.length()) end = mid - 1;

            else if (inArray[mid].compareTo(search) < 0) start = mid + 1;

            else end = mid-1;

        }

        if(found) Answer+="True";

        else Answer+="False";

        return Answer;

    }

    public static void main(String []args){

        String [] Array = {"a","aaa","aaaaa"};

        System.out.println(binarySearchLengths(Array,"bb",0,2));

        String []Array2 = {"a","b","c","d","e","f","g"};

        System.out.println(binarySearchLengths(Array2,"e",0,6));

    }

}


Related Solutions

JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print the array double the value in the array at position start recMethod(array, start+1, end) print the array } else { print "done" } } Assume the method is invoked with array = [3, 4, 6, 7, 8, 10, 4], start = 2, and end = 5 1.How many times is the array printed before the word "done" is printed? 2.How many times is the...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
void printList(double * A, int start, int end ) { if (start == end) //base case...
void printList(double * A, int start, int end ) { if (start == end) //base case { cout << A[start] << endl; } else //recursive case { cout << A[start] << endl; printList(A, start + 1, end); } } int main() { double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 }; printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6 cout << endl; //HELP HERE: Using a recursive method on C++,...
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
How can I convert the following subroutine into MIPS assembly: search(Array, start, end, searchValue) { if...
How can I convert the following subroutine into MIPS assembly: search(Array, start, end, searchValue) { if (start > end) return false mid = start + (end - start)/2; if (array[mid] == searchValue) return true; if (array[mid] > searchValue) return search(array, start, mid-1, searchValue); return search(array, mid+1, end, searchValue); }
Write a static method remove(int v, int[] in) that will return a new array of the...
Write a static method remove(int v, int[] in) that will return a new array of the integers in the given array, but with the value v removed. For example, if v is 3 and in contains 0, 1, 3, 2, 3, 0, 3, and 1, the method will return an array containing 0, 1, 2, 0, and 1. Hint: You can follow two steps to solve this problem: Create an array in the method, let say you called it result....
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
Design a class named ArraySort and it contains the following method: Public int search(int target): if...
Design a class named ArraySort and it contains the following method: Public int search(int target): if the target is found in the array, return the number of its showing up. If the target is not found in the array, return -1. If the array is empty, return -1; Public int maximum():Return the maximum number in the array if the array is nonempty, otherwise return -1; Public int minimum(): Return the minimum number in the array if the array is nonempty,...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT