I created a recursive function that uses binary search to just return true if it finds the value and false if it does not.
I'm new to recursion and binary search so please let me know where I can improve upon.
/**
* Uses binary search O(log n). Returns true if the values is in the value array false if it's not. Recursive
*/
bool binarySearch( int value, int values[], int left, int right ) {
//keep track of when the array will be empty and return false
if ( right < left ) {
return false;
}
//Find the middle of the array
int mid = ( left + right ) / 2;
//compare the value to the middle of the array. If it's a match return true, else move the left position and right position accordingly
if( value == values[ mid ] ) {
return true;
} else if ( value < values[ mid ] ) {
right = mid - 1;
} else {
left = mid + 1;
}
//return the function
return binarySearch( value, values, left, right );
}