Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define mx 5
- int search(int arr[],int x,int length)
- {
- int li=0,hi=length-1;
- while(li<=hi)
- {
- int mid=(li+hi+1)/2;
- //cout<<mid;
- if(arr[mid]==x)
- return mid;
- if(arr[mid]>x)
- hi=mid-1;
- else
- li=mid+1;
- }
- return -1;
- }
- int main()
- {
- int arr[5]={ 10,20 ,30,40,50};
- int value;
- cout<<"Which Value you want to find"<<endl;
- cin>>value;
- int length=sizeof(arr);
- int result=search(arr,value,length);
- if(result==-1)
- cout<<"Element is not present in array";
- else
- cout<<"Element is present at index"<<" "<<result;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement