Advertisement
Riz1Ahmed

Binary Search

Feb 19th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include <cstdio>
  2. int main(){
  3.     int n;
  4.     printf("Input array size: ");
  5.     scanf("%d",&n);
  6.     int a[n],f;
  7.     printf("Input the array values 1 by 1:\n");
  8.     for (int i=0; i<n; i++) scanf("%d",&a[i]);
  9.     printf("Input the Search value: ");
  10.     scanf("%d",&f);
  11.     ///Binary search start.
  12.     int l=0,r=n-1,m;
  13.     while(l<=r){
  14.         m=l+(r-1/2);
  15.         if (a[m]==f)
  16.             return!printf("Found at %d no index\n",m);
  17.         else if (a[m]>n) r=m-1;
  18.         else l=m+1;
  19.     }
  20.     printf("The value not found\n");
  21.     return 0;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement