Concept explainers
Explanation of Solution
Modified Recursive binary search
The modified recursive binary search method is given below:
//Define the binarySearch() method
public static int binarySearch(int[ ] data, int target, int low, int high)
{
//Check "low" is greater than "high"
if (low > high)
//Return the -1
return −1;
//Execute the else part
else
{
/*Add "low" and "high" and the result is divided by 2 and then store the result element to "mid". */
int mid = (low + high) / 2;
//Check "target" is equal to "data[mid]"
if (target == data[mid])
//Return the index of search element
return mid;
//Execute the else part
else if (target < data[mid])
//Call the binarySearch() method
return binarySearch(data, target, low, mid − 1);
//Execute the else part
;&#x...
Learn your wayIncludes step-by-step video
Chapter 5 Solutions
Data Structures and Algorithms in Java
- Given the following non-recursive implementation of depth-first search: A. Complete the implementation of depth-first search by filling in the TODO sections with the appropriate C++ code. Remember to: Print out each node you visit. Visit each node exactly once.arrow_forwardOne-friend recursion vs iteration.1. Your task is to accept a tuple a1, a2, ... , an and return the reversed tuple an, an−1, ... , a1. Being lazy, you will only strip off an element from one end or add an element back onto one end. But you have recursive friends to help you. Provide both a paragraph containing the friend’s explanation of the algorithm, and the recursive code.2. Now suppose that you have a stack, but no friends. (See Chapter 3). Quickly sketch an iterative program that solves this same problem. Be sure to include loop invariants and other the key steps required for describing an iterative algorithm.3. Trace each of these two programs. Step by step, compare and contrast their computations on a computer. i already uploaded this question 3 times and getting wrong answer so if you don't know right answer dont write rubbih ans otherwise you will get downvote don't copy bartleby old or chegg its wrongarrow_forwardOne-friend recursion vs iteration.1. Your task is to accept a tuple a1, a2, ... , an and return the reversed tuple an, an−1, ... , a1. Being lazy, you will only strip off an element from one end or add an element back onto one end. But you have recursive friends to help you. Provide both a paragraph containing the friend’s explanation of the algorithm, and the recursive code.2. Now suppose that you have a stack, but no friends. (See Chapter 3). Quickly sketch an iterative program that solves this same problem. Be sure to include loop invariants and other the key steps required for describing an iterative algorithm.3. Trace each of these two programs. Step by step, compare and contrast their computations on a computer. i already uploaded this question 3 times and getting wrong answer so if you don't know right answer dont write rubbih ans otherwise you will get downvotearrow_forward
- Describe not just what the stopping condition for the recursive binary search is but also why it is essential for recursive algorithms to always have a stopping condition.arrow_forwardWrite a recursive binary search procedure SEARCH (a, f, k) which looks for the element a at locations f, f+ 1, f+2,... k of an array A withn elements in set Sarrow_forwardGive a recursive algorithm for the sequential search and explain its running time.arrow_forward
- Write code to modify the recursive sort also to do the closest-point computation when pass is 2?arrow_forwardg1 = 5 g2 = 6 for k in range(3,8): gk = (k-1)·gk-1 + gk-2 What is the last term, g8, of the recursive sequence generated as a result of executing this algorithm?arrow_forwardOne-friend recursion vs iteration.1. Your task is to accept a tuple a1, a2, ... , an and return the reversed tuplean, an−1, ... , a1. Being lazy, you will only strip off an element from one end oradd an element back onto one end. But you have recursive friends to help you.Provide both a paragraph containing the friend’s explanation of the algorithm,and the recursive code.2. Now suppose that you have a stack, but no friends. (See Chapter 3). Quickly sketchan iterative program that solves this same problem. Be sure to include loop invariants and other the key steps required for describing an iterative algorithm.3. Trace each of these two programs. Step by step, compare and contrast their computations on a computer.arrow_forward
- There is an error in the following recursive method, which implements a binary search. Find the error and indicate how to fix the code so that the method will work correctly.arrow_forwardExplain the halting condition for the recursive binary search and why all recursive algorithms need one.arrow_forwardThe Binary Search algorithm works by testing a mid-point, then eliminating half of the list. In this exercise, you are going to take our binary search algorithm and add print statements so that you can track how the search executes. Inside of the recursive binary search function, add print statements to print out the starting, ending, and midpoint values each time. Then as you test a value, print out the results, either too high, too low, or a match. Sample Output Starting value: 0 Ending value: 9 Testing midpoint value: 4 Too high! Starting value: 0 Ending value: 3 Testing midpoint value: 1 Too low! Starting value: 2 Ending value: 3 Testing midpoint value: 2 Match! public class BinaryExplorer { public static void main(String[] args) {int[] testArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; binaryRec(testArray, 8, 0, testArray.length - 1); } /*** Add Print statements to the binaryRec method:* * Print Starting, ending, and midpoint values.* * Print when you find a match* * Print if you are…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education