Fill in the Recursive Binary Search private int binarySearchR (int target, int l
ID: 3568738 • Letter: F
Question
Fill in the Recursive Binary Search
private int binarySearchR (int target, int lo, int hi)
{
int index;
if ( ____________________ ) // fill in the "not found" condition
index = -1;
else
{
int mid = (lo + hi)/2;
if ( _______________________ ) // found it!
index = mid;
else if (target < list[mid])
// fill in the recursive call to search the first half
// of the list
index = ________________________________________________;
else
// search the last half of the list
index = _______________________________________________;
}
return index;
}
Explanation / Answer
private int binarySearchR (int target, int lo, int hi)
{
int index;
if ( lo > hi ) // fill in the "not found" condition
index = -1;
else
{
int mid = (lo + hi)/2;
if ( target == mid ) // found it!
index = mid;
else if (target < list[mid])
// fill in the recursive call to search the first half
// of the list
hi = mid - 1;
binarySearchR (target,lo, hi);
else
lo = mid + 1;
binarySearchR (target,lo, hi)
}
return index;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.