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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 21.06.2019 20:20, ab847150
Wireless communications is likely to be viewed as an essential part of an enterprise network infrastructure when: select one: a. mobile communication is needed b. communication facilities must be installed at low initial cost c. communication must take place in a hostile or difficult terrain that makes wired communication difficult or impossible d. the same information must be broadcast to many locations
Answers: 1
image
Computers and Technology, 22.06.2019 22:30, jacob7542
The qwerty keyboard is the most common layout of keys on a keyboard
Answers: 3
image
Computers and Technology, 23.06.2019 15:00, hunteryolanda82
Based on the current economic situation do you expect the employment demand for graduating engineers to increase or decrease? explain the basis for your answer. with a significant economic recovery, what do you think will happen to future enrollments in graduating engineering programs?
Answers: 1
image
Computers and Technology, 23.06.2019 18:00, yedida
File account. java (see previous exercise) contains a definition for a simple bank account class with methods to withdraw, deposit, get the balance and account number, and return a string representation. note that the constructor for this class creates a random account number. save this class to your directory and study it to see how it works. then write the following additional code: 1. suppose the bank wants to keep track of how many accounts exist. a. declare a private static integer variable numaccounts to hold this value. like all instance and static variables, it will be initialized (to 0, since it’s an int) automatically. b. add code to the constructor to increment this variable every time an account is created. c. add a static method getnumaccounts that returns the total number of accounts. think about why this method should be static - its information is not related to any particular account. d. file testaccounts1.java contains a simple program that creates the specified number of bank accounts then uses the getnumaccounts method to find how many accounts were created. save it to your directory, then use it to test your modified account class.
Answers: 3
You know the right answer?
The Binary Search algorithm works by testing a mid-point, then eliminating half of the list. In thi...

Questions in other subjects:

Konu
Mathematics, 29.04.2021 01:00
Konu
Mathematics, 29.04.2021 01:00
Konu
Mathematics, 29.04.2021 01:00