Recursion We have learned the binary search algorithm this semester, but we implement binary search using a while loop. In this exercise, we are going to see binary search implemented by recursion and trace the recursion. Tracing the Recursion. Observe the recursive solution provided below and answer the following questions: 1. Which line(s) of this program define(s) the base case of the binary() method? 2. Which line(s) of this program include recursive call(s)? 3. Trace the recursion below. You must show the tracing step by step (write them down); otherwise – little to no credit! 4. At what step of your recursion tracing did you hit the base case? 5. What is the final output of this code?

icon
Related questions
Question
Recursion
We have learned the binary search algorithm this semester, but we implement
binary search using a while loop. In this exercise, we are going to see binary
search implemented by recursion and trace the recursion.
Tracing the Recursion. Observe the recursive solution provided below and answer the following
questions:
1. Which line(s) of this program define(s) the base case of the binary() method?
2. Which line(s) of this program include recursive call(s)?
3. Trace the recursion below. You must show the tracing step by step (write them
down); otherwise – little to no credit!
4. At what step of your recursion tracing did you hit the base case?
5. What is the final output of this code?
 
 
 
 
 
1 public class binarySearch {
2
30
4
5
6
7
8
9
10
11
12
13
14
15
16
16
176
10
18
10
19
20
20
21
22
23
24
25
26}
}
int mid (left + right) / 2;
if (arr [mid] == target) {
public static int binary(int[] arr, int target, int left, int right) {
if (left right) {
return -1;
return mid;
} else if (arr[mid] > target) {
return binary(arr, target, left, mid - 1);
Page <
} else {
return binary(arr, target, mid + 1, right);
}
4
}
public static void main(String[] args) {
int[] arr= {1, 2, 3, 4, 5, 6, 7);
int target = 2;
System.out.println (binary(arr, target, 0, arr.length - 1));
of 4
ZOOM
+
*k
Transcribed Image Text:1 public class binarySearch { 2 30 4 5 6 7 8 9 10 11 12 13 14 15 16 16 176 10 18 10 19 20 20 21 22 23 24 25 26} } int mid (left + right) / 2; if (arr [mid] == target) { public static int binary(int[] arr, int target, int left, int right) { if (left right) { return -1; return mid; } else if (arr[mid] > target) { return binary(arr, target, left, mid - 1); Page < } else { return binary(arr, target, mid + 1, right); } 4 } public static void main(String[] args) { int[] arr= {1, 2, 3, 4, 5, 6, 7); int target = 2; System.out.println (binary(arr, target, 0, arr.length - 1)); of 4 ZOOM + *k
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer