fork download
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n=8; // no of elements in the array
  6. int a[]={5,11,23,30,35,50,60,75}; // array
  7. int key=35; // element to be searched
  8. int s=0,e=7;
  9. int f=0;
  10. while(s<=e)
  11. {
  12. int mid=(s+e)/2;
  13. if(key==a[mid])
  14. {
  15. cout<<"element found"<<endl;
  16. f=1;
  17. break;
  18. }
  19. else if(a[mid]<key)
  20. {
  21. s=mid+1;
  22. }
  23. else
  24. {
  25. e=mid-1;
  26. }
  27. }
  28. if(f==0)
  29. {
  30. cout<<"Not Found"<<endl;
  31. }
  32. return 0;
  33. }
Success #stdin #stdout 0s 4388KB
stdin
Standard input is empty
stdout
element found