int binarySearch( int SortedArray[ ] , int key ){
if( SortedArray.length == 0 )
throw new ItemNotFound( "Binary Search fails: empty" );
int low = 0;
int high = SortedArray.length - 1;
int mid;
while( low < high )
{
mid = ( low + high ) / 2;
if( SortedArray[mid] < key )
low = mid + 1;
else
high = mid;
}
if( SortedArray[low] == key)
return low;
throw new ItemNotFound( "BinarySearch fails" );
}
What is the easiest way to make this one comparison per level Binary Search recursive? Thanks for any help.
2007-02-19
08:48:46
·
2 answers
·
asked by
Eric H
2
in
Computers & Internet
➔ Programming & Design