Computers and Technology

The 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 too high or too low.
*
**/
public static int binaryRec(int[] array, int target, int begin, int end) {
if (begin <= end)
{
int mid = (begin + end) / 2;
// Base Case
if (target == array[mid]) {
return mid;
}
if (target < array[mid]) {
return binaryRec(array, target, begin, mid - 1);
}
if (target > array[mid]) {
return binaryRec(array, target, mid + 1, end);
}
}
return -1; //Alternate Base Case - not found
}
}

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 14:20, babyrocks7300
Consider a byte-addressable computer with 16mb of main memory, a cache capable of storing a total of 64kb of data and block size of 32 bytes. (a) how many bits in the memory address? (b) how many blocks are in the cache? (c) specify the format of the memory address, including names and sizes, when the cache is: 1. direct-mapped 2. 4-way set associative 3. fully associative
Answers: 2
image
Computers and Technology, 22.06.2019 19:30, 710jonathan
The following is an excerpt from a slide presentation. today we will inverse operations solving equations using inverse operations solving inequalities using inverse operations from which part of the presentation does the slide most likely come from? a. introduction b. outline c. body d. conclusion
Answers: 1
image
Computers and Technology, 24.06.2019 01:00, arturocarmena10
The initial tableau of a linear programming problem is given. use the simplex method to solve it. x 1 x 2 x 3 s 1 s 2 z 1 2 4 1 0 0 8 3 4 1 0 1 0 10 minus3 minus12 1 0 0 1 0 the maximum is nothing when x 1equals nothing, x 2equals nothing, x 3equals nothing, s 1equals3, and s 2equals0. (be sure to simplify to lowest terms if necessary.)
Answers: 2
image
Computers and Technology, 24.06.2019 18:30, WATBOI01
What is the local portion of the e-mail address below? twrigley@gumchewer. biz a.) @ b.) biz c.) gumchewer d.) twrigley
Answers: 1
Do you know the correct answer?
The Binary Search algorithm works by testing a mid-point, then eliminating half of the list. In thi...

Questions in other subjects:

Konu
Biology, 30.04.2021 17:30