Advertisement
shabbyheart

Untitled

Apr 7th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define mx 5
  4. int search(int arr[],int x,int length)
  5. {
  6. int li=0,hi=length-1;
  7. while(li<=hi)
  8. {
  9. int pos=li + ((x-arr[li])*(hi-li) / (arr[hi]-arr[li]) );
  10. //cout<<mid;
  11. if(arr[pos]==x)
  12. return mid;
  13. if(arr[pos]>x)
  14. hi=pos-1;
  15. else
  16. li=pos+1;
  17. }
  18. return -1;
  19. }
  20. int main()
  21. {
  22. int arr[5]={ 10,50 ,40,30,20};
  23. sort(arr,arr+5);
  24. int value;
  25. cout<<"Which Value you want to find"<<endl;
  26. cin>>value;
  27. int length=sizeof(arr);
  28. int result=search(arr,value,length);
  29. if(result==-1)
  30. cout<<"Element is not present in array";
  31. else
  32. cout<<"Element is present at index"<<" "<<result;
  33. return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement