Advertisement
shabbyheart

Untitled

Apr 7th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 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 mid=(li+hi+1)/2;
  10. //cout<<mid;
  11. if(arr[mid]==x)
  12. return mid;
  13. if(arr[mid]>x)
  14. hi=mid-1;
  15. else
  16. li=mid+1;
  17. }
  18. return -1;
  19. }
  20. int main()
  21. {
  22. int arr[5]={ 10,20 ,30,40,50};
  23. int value;
  24. cout<<"Which Value you want to find"<<endl;
  25. cin>>value;
  26. int length=sizeof(arr);
  27. int result=search(arr,value,length);
  28. if(result==-1)
  29. cout<<"Element is not present in array";
  30. else
  31. cout<<"Element is present at index"<<" "<<result;
  32. return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement