Skip to content

Instantly share code, notes, and snippets.

@ahmedengu
Created March 9, 2016 04:05
Show Gist options
  • Save ahmedengu/03d64bc0e8007f019406 to your computer and use it in GitHub Desktop.
Save ahmedengu/03d64bc0e8007f019406 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
void binarysearch(int target)
{
int arr[] = {1 ,1 ,3 ,4 ,5 ,5 ,6 ,7 ,7 ,8 ,9 ,9};
int size=12;
int f,l,mid,c;
f=0,l=size-1;
while(f<=l)
{
mid=(f+l)/2;
if(target==arr[mid])
{
cout<<endl<<"found at :"<<mid;
return;
}
else if(target<arr[mid])
{
l=mid-1;
}
else
f=mid+1;
}
cout<<"not found";
}
int main() {
binarysearch(5);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment