Created
March 9, 2016 04:05
-
-
Save ahmedengu/03d64bc0e8007f019406 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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