Add a recursive implementation of the bindary search algorithm to the Searching
ID: 3656971 • Letter: A
Question
Add a recursive implementation of the bindary search algorithm to the Searching class. Create a driver to demonstrate this implementationExplanation / Answer
public class BinarySearchRecursive { public static final int NOT_FOUND = -1; /** * Performs the standard binary search * using two comparisons per level. * This is a driver that calls the recursive method. * @return index where item is found or NOT_FOUND if not found. */ public static int binarySearch( Comparable [ ] a, Comparable x ) { return binarySearch( a, x, 0, a.length -1 ); } /** * Hidden recursive routine. */ private static int binarySearch( Comparable [ ] a, Comparable x, int low, int high ) { if( low > high ) return NOT_FOUND; int mid = ( low + high ) / 2; if( a[ mid ].compareTo( x ) < 0 ) return binarySearch( a, x, mid + 1, high ); else if( a[ mid ].compareTo( x ) > 0 ) return binarySearch( a, x, low, mid - 1 ); else return mid; } // Test program public static void main( String [ ] args ) { int SIZE = 8; Comparable [ ] a = new Integer [ SIZE ]; for( int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.