Since binary search is very important, we will have a look at a method that uses recursion. The purpose of a binary search is to search an array and find out whether it contains a specific value. The array used to conduct a binary search is a sorted array. The search is conducted by splitting the original array into two halves. Since the initial array is sorted, one can determine the half in which the desired number might be found. Once one of the two halves is selected as a candidate, we will go through the same process until the last split, which will leave us with two numbers. One of these two numbers might be the one we are searching for. Here is the pseudocode for a binary search:

int a[some_Size_Value];

Algorithm to search a[first] through a[last]

// Precondition - sorted array

// a[first] <= a[first + 1] <= a[first + 2] <= ... <= a[last]

To locate the value key:

if( first > last) // a stopping case

found = false;

else

{

mid = approximate midpoint
between first and last;

if(key == a[mid]) // A stopping
case

{

found = true;

location = mid;

}

else if( key < a[mid]
) // a case with recursion

search a[first] through a[mid - 1];

else if( key > a[mid] )
// a case with recursion

search a[mid + 1] through a[last];

}

The recursive function for binary search is shown on Display 13.6 of
the textbook.