Advertisement
Md_hosen_zisad

binary s up

Mar 4th, 2018
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int i,a[100],n,m,low,high,s;
  6.     cin>>n;
  7.     low=1,high=n;
  8.  
  9.     for(i=0;i<n;i++)
  10.     {
  11.         cin>>a[i];
  12.  
  13.     }
  14.     cout<<"search";
  15.     cin>>s;
  16.     while(low<=high)
  17.     {
  18.         m=(low+high)/2;
  19.         if(s==a[m])
  20.         {
  21.             break;
  22.         }
  23.         else if(s<a[m])
  24.           {
  25.  
  26.            high=m-1;}
  27.  
  28.     else if(s>a[m])
  29.     {
  30.         low=m+1;
  31.     }}
  32.     if(low>high)
  33.     cout<<"not found";
  34.     else
  35.         cout<<"found";
  36.  
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement