Question

In: Computer Science

In the recursive version of binary search each function call reduces the search size by half....

In the recursive version of binary search each function call reduces the search size by half. Thus in the worst case for a sorted list of length n. The algorithm makes log n calls. Is it possible to make fewer than log n calls?

Group of answer choices

1) Yes, depends on when it finds the element it is looking for

2) No, it will always make log n calls.

Solutions

Expert Solution

OPTION (1) Yes, depends on when it finds the element it is looking for !!

REASON:

In the binary search we will get a sorted array and an element to search for.

We search for the middle element whether that is the required key.

  1. If the middle element is Key, return the index of that middle element.
  2. Else if the middle element is less than the key, we search in the right half only.
  3. Else we search in the LEFT part only.

For example, take n=11 elements.

list = [1,2,3,4,5,6,7,8,9,10,11] are the list of elements.

We want to search for 6. key=6

Now, the middle element is list[(0+11)/2]=list[5]=6

So, we found the key in the first search itself,!! We will stop the search process.

Hence, we just need lest than log2(11) searches that is 4.

Hence, it depends on when it finds the element it is looking for !!


Related Solutions

Java String search Design and implement a recursive version of a binary search.  Instead of using a...
Java String search Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are...
Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive version of...
Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive version of a binary search. Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time. If the value is not the target, refine the search space and call the method again. The name to search for is entered by the user, as is the indexes that define the range of viable...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
Write a version of the binary search algorithm that can be used to search a string...
Write a version of the binary search algorithm that can be used to search a string vector object. Also, write a program to test your algorithm. (Use the selection sort algorithm you developed in Programming Exercise 12 to sort the vector.) Your program should prompt the user to input a series of strings, ending the input stream with zzz. The program should then prompt the user to search for a specific string in the list. If the string is found...
Question 2 (Function Template) Write a template version of the iterative binary search algorithm that searches...
Question 2 (Function Template) Write a template version of the iterative binary search algorithm that searches an array of arbitrary type for a given key. Declare and implement a class called Student that keeps the student id, name, and grade. Include a default constructor, the overloaded insertion (<<) operator and also the overloaded extraction operator (>>). Declare and implement another class called Book that keeps the book’s title, author, and price. Just like the Student class, Include in class Book...
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Implement a recursive binary search on an array of 8-byte integers in LEGV8
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
For C++ Function 1: Write a recursive function to perform a sequential search on a set...
For C++ Function 1: Write a recursive function to perform a sequential search on a set of integers The function will require an array parameter and the number to look for. These are the minimal parameter requirements The function should take an array of any size Function 2: Write a recursive function that will convert an integer (base 10) to binary The function should only have an integer parameter Have the function write the binary number to the console You...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT