subject

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
}
}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 18:00
Amara created a workbook to track the number of minutes she reads each week. each day, she entered the number of minutes into the workbook. identify the types of data in the workbook using the drop-down menus.
Answers: 3
question
Computers and Technology, 22.06.2019 14:40
You begin your first day of responsibilities by examining the recent is security breach at gearup to get ideas for safeguards you will take. at gearup, criminals accessed the company's improperly-secured wireless system and stole customers' credit card information as well as employee social security numbers. what kind of computer crime did gearup face?
Answers: 3
question
Computers and Technology, 23.06.2019 16:30
How to do this programming flowchart?
Answers: 3
question
Computers and Technology, 23.06.2019 18:30
Janice recently received her college degree and is looking for a job. she is worried that since she just finished school, she will be required to repay her perkins and direct subsidized loans immediately. janice pulls out the paperwork she signed and reviews it again for repayment information. after reading all of the information, janice discovers that
Answers: 2
You know the right answer?
The Binary Search algorithm works by testing a mid-point, then eliminating half of the list. In thi...
Questions
question
Mathematics, 18.10.2020 08:01
question
Social Studies, 18.10.2020 08:01
question
History, 18.10.2020 08:01
question
Social Studies, 18.10.2020 08:01
question
Mathematics, 18.10.2020 08:01
question
Mathematics, 18.10.2020 08:01
Questions on the website: 13722362