// Online C++ compiler to run C++ program online #include <iostream> using namespace std; int binarySearch(int arr[], int size , int key) { int start = 0; int end = size-1; int mid = (start+end) / 2; while(start<=end){ if(arr[mid] == key ){ return mid ; } // go to right wala part if(key > arr[mid]) { start = mid + 1; } else { end = mid -1 ; } mid =(start + end )/2; } return -1; } int main() { int even[6] ={2, 4, 8 , 10, 12 , 18}; int odd[5] = { 3 , 5 , 7, 9, 11}; int index = binarySearch(even , 6 ,10); cout << "index of 10 " << index << endl; int in = binarySearch(odd , 5 ,7); cout << "index of 7 " << in << endl; return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter