Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote