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 pos=li + ((x-arr[li])*(hi-li) / (arr[hi]-arr[li]) );
- //cout<<mid;
- if(arr[pos]==x)
- return mid;
- if(arr[pos]>x)
- hi=pos-1;
- else
- li=pos+1;
- }
- return -1;
- }
- int main()
- {
- int arr[5]={ 10,50 ,40,30,20};
- sort(arr,arr+5);
- 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