Here is the code for a recursive method for binary search that is searching a sorted array of ints. The array is assumed to be sorted in ascending order.
// we are looking for the value stored in the parameter key
public static int binarySearch ( int [ ] arr, int key, int start,
int end )
{
System.out.println( "Start = " + start + "; end = " + end );
if ( start <= end )
{
int middle = ( start + end ) / 2;
if ( arr[middle] == key ) // found it at index middle
return middle;
else if ( arr[middle] > key ) // look left
return binarySearch( arr, key, start, middle - 1 );
else // look right
return binarySearch( arr, key, middle + 1, end );
}
else // not found
return -1;
}
We are running the following code:
int [ ] numbers = { 4, 6, 8, 10, 11, 12, 14, 16, 18 };
int found = binarySearch( numbers, 10, 0, 8 );
What is the output? Show what the output statement in the binarySearch method outputs. The question is not about the value of found; the value of found is 3.
Correct Answer:
Verified
S...
View Answer
Unlock this answer now
Get Access to more Verified Answers free of charge
Q100: Code a recursive method that takes a
Q101: Consider the following state of a
Q102: Consider the following state of a
Q103: Assume that a PlayerNode class has been
Q104: Assume that a PlayerNode class has been
Q105: Here is an example of a stack
Q106: In the code below, how many times
Q107: A method is coded as follows:
public static
Q108: An algorithm has the following formula for
Q109: A method is coded as follows:
public static
Unlock this Answer For Free Now!
View this answer and more for free by performing one of the following actions
Scan the QR code to install the App and get 2 free unlocks
Unlock quizzes for free by uploading documents