#include using namespace std; //Will return the index of the element if found //else will return -1 . int binarySearch(const int arr1[], int numElems, int value) { int leftIndex = 0 ; int rightIndex = numElems - 1 ; int result = -1 ; int middleIndex = 0 ; while( leftIndex <= rightIndex ) { middleIndex = ( leftIndex + rightIndex ) / 2 ; if ( arr1[middleIndex] == value ) return middleIndex ; else if ( arr1[middleIndex] < value ) { leftIndex = middleIndex + 1 ; } else { rightIndex = middleIndex - 1 ; } } //while return result ; } int main() { int arr1[] = { 1, 2, 3, 5, 7 } ; cout << binarySearch( arr1 , 5 , 3 ) << endl ; cout << binarySearch( arr1 , 5 , 8 ) << endl ; }