Question

Given the following code for a binary search, how many times this method have to be...

Given the following code for a binary search, how many times this method have to be executed in order to find the number 5?

A) 1 time

B) 2 times

C) 3 times

D) 4 times

E) more than 5 times

public class BinarySearch{

public static void main(String []args){
if (right >= left) {

int middle = left + (right - left) / 2;

if (arr[middle] == num)

return middle;

if (arr[mid] > num)

return binarySearch(arr, left, middle - 1, num);

return binarySearch(arr, middle + 1, right, num);

}

return -1;
  
}

int[] arr = {1, 3, 5, 6, 7, 9, 11, 15, 18}

return binarySearch(arr, 0, 8, 5)

int binarySearch(int arr[], int left, int right, int num)

{

  

}
}


  


0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class BinarySearch {

    static int count = 0;

    public static void main(String[] args) {
        int[] arr = {1, 3, 5, 6, 7, 9, 11, 15, 18};
        System.out.println(binarySearch(arr, 0, 8, 5));
        System.out.println("Number of times binarySearch is executed is " + count);
    }

    static int binarySearch(int arr[], int left, int right, int num) {
        ++count;
        if (right >= left) {
            int middle = left + (right - left) / 2;
            if (arr[middle] == num)
                return middle;
            if (arr[middle] > num)
                return binarySearch(arr, left, middle - 1, num);
            return binarySearch(arr, middle + 1, right, num);
        }
        return -1;
    }
}

C) 3 times

Add a comment
Know the answer?
Add Answer to:
Given the following code for a binary search, how many times this method have to be...
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
  • Have to write the tree into a text file? JAVA CODE Binary search tree This is...

    Have to write the tree into a text file? JAVA CODE Binary search tree This is the tree public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left;...

  • Problem: The code I have below compiled/debugged using C#, however I need to measure the time...

    Problem: The code I have below compiled/debugged using C#, however I need to measure the time it takes to do the 10,000,000 searches for each of the eight arrays. Could you compare the timings to the theoretical timings the algorithm binary search provides. Instructions prior to this: Implement a binary search function in C#. In this program we are to carry out the same 10,000,000 unsuccessful searches for eight different-sized arays, namely arrays of sizes 128, 512, 2,048, 8,192, 32,768,...

  • Given driver method. Complete the following Linear search method where a given element num in list[...

    Given driver method. Complete the following Linear search method where a given element num in list[ ] Class search public static int search(int list[], int num) public static void main(String args[]) { int list[] = {12, 31, 4, 100,4%; int num = 10; int result = search(list, num); if(result == -1) System.out.print("Search key is not found"); else System.out.print("Search key is present at index" + result);

  • plz solve Modify binary search so that it always returns the element with the smallest index...

    plz solve Modify binary search so that it always returns the element with the smallest index * that matches the search element (and still guarantees logarithmic running time). import java.util.Arrays; public class BinarySearch2 { public static int rank(int key, int[] a) { // Array must be sorted. int lo = 0; int hi = a.length - 1; while (lo <= hi) { // Key is in a[lo..hi] or not present. int mid = lo + (hi - lo) / 2;...

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

  • In C++, make the following binary search function work on an array of strings to give...

    In C++, make the following binary search function work on an array of strings to give you the index location of the string you want in the array. It currently works on integers only. int binarySearch(int arr[], int firstIndex, int lastIndex, int target){ int index; if (firstIndex>lastIndex) index = -1; else { int mid = firstIndex + (lastIndex - firstIndex) / 2; if (target == arr[mid]) index = mid; else if (target < arr[mid]) index = binarySearch(arr, firstIndex, mid -...

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

  • I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at...

    I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at all, it is just left blank. package edu.wit.cs.comp1050; /** * Solution to the third programming assignment * When it runs it outputs the amount in quarters, dimes, nickels and pennies * * @author Rose Levine * */ import java.util.Scanner; public class PA1c {       /**    * Error message to display for negative amount    */    public static final String ERR_MSG =...

  • (file ToDo.java) Add code for an equals method that checks the description of a ToDo item...

    (file ToDo.java) Add code for an equals method that checks the description of a ToDo item with a passed ToDo item’s description. The method should return true if they are identical and false if the descriptions are not identical. public class Lab8Interfaces {    /**    * @param args    * the command line arguments    */    public static void main(String[] args) {        ToDoItem[] arr = new ToDoItem[5];        ToDoItem toDo1 = new ToDoItem("Hospital Project");   ...

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

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