void BinarySearch(const int A[], int First, int Last, int Value, int& Index) // --------------------------------------------------------- // Searches the array items A[First] through A[Last] // for Value by using a binary search. // Precondition: 0 <= First, Last <= SIZE-1, where // SIZE is the maximum size of the array, and // A[First] <= A[First+1] <= ... <= A[Last]. // Postcondition: If Value is in the array, Index is // the index of the array item that equals Value; // otherwise Index is -1. // --------------------------------------------------------- { if (First > Last) Index = -1; // Value not in original array else { // Invariant: If Value is in A, // A[First] <= Value <= A[Last] int Mid = (First + Last)/2; if (Value == A[Mid]) Index = Mid; // Value found at A[Mid] else if (Value < A[Mid]) BinarySearch(A, First, Mid-1, Value, Index); // X else BinarySearch(A, Mid+1, Last, Value, Index); // Y } // end else } // end BinarySearch