// if searchKey matches middle
element, return middle
if ( searchKey == b[ middle ] ) // match
return middle;
else
// if searchKey less than middle
element,
// set new high element
if ( searchKey < b[ middle ] )
high = middle - 1; // search low end of array
// if searchKey greater than middle
element,
// set new low element
else
low = middle + 1; // search high end of array
}
return -1;
// searchKey not found
} // end function binarySearch
Use
the rule of binary search:
If
key equals middle, match
If less, search low end
If greater, search high end
Loop sets low, middle and high dynamically. If searching the high end, the new low is the element above the middle.