#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[], int size, int key)
{
int start=0;
int end=size-1;
int mid=start+(start-end)/2; //for avoiding excess memory
while(start<=end)
{
if(arr[mid]==key) return mid;
if(key>arr[mid])
{
start=mid+1;
}
else
{
end=mid-1;
}
mid=start+(start-end)/2;
}
return -1;
}
int main() {
int even[6]={2,4,6,8,12,18};
int odd[5]={3,8,11,14,17};
int index=binarySearch(odd, 5, 95);
cout<<"index of 95 is: "<<index;
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