Algorithms/binary_search/binary_search.cpp
int binary_search(vector<int> arr, int key)
{
    int s = 0 , e = arr.size() - 1 ;
    while (s <= e)
    {
        int mid = (s+e)/2 ;
        if (arr[mid] == key) return mid ;

        if (arr[mid] < key) s = mid + 1 ;
        else e = mid - 1 ;
    }
    return -1 ;
}
// Array is sorted. Find mid element if it is equal to key return mid
// else if key is smaller than mid element then key if present is present in left part
// if key is greater than mid then key if present is present in right part
Array is sorted. Find mid element if it is equal to key return mid
else if key is smaller than mid element then key if present is present in left part
if key is greater than mid then key if present is present in right part
Array is sorted . Find mid element if it is equal to key return mid else if key is smaller than mid element then key if present is present in left part if key is greater than mid then key if present is present in right part
int binary_search(vector<int> arr, int key)
{
    int s = 0 , e = arr.size() - 1 ;
    while (s <= e)
    {
        int mid = (s+e)/2 ;
        if (arr[mid] == key) return mid ;
        if (arr[mid] < key) s = mid + 1 ;
        else e = mid - 1 ;
    }
    return -1 ;
}
int binary_search ( vector < int > arr , int key ) { int s = 0 , e = arr . size ( ) - 1 ; while ( s <= e ) { int mid = ( s + e ) / 2 ; if ( arr [ mid ] == key ) return mid ; if ( arr [ mid ] < key ) s = mid + 1 ; else e = mid - 1 ; } return -1 ; }
Array is sorted.
Array is sorted .