Advertisement
Tusohian

Binary Search

Jul 21st, 2017
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int n, arr[100], bg, en, md, i, itm, loc=1;
  7.  
  8.     cout<<"Please Enter The Element Number: ";
  9.     cin>>n;
  10.  
  11.     bg=1;
  12.     en=n;
  13.     md=int((bg+en)/2);
  14.  
  15.     cout<<"Please Enter Data One By One by Pressing Enter: ";
  16.  
  17.     for(i=1; i<=n; i++)
  18.         cin>>arr[i];
  19.  
  20.     cout<<"Please Enter The ITEAM you want to search: ";
  21.     cin>>itm;
  22.  
  23.     while (bg<=en && arr[md] != itm)
  24.     {
  25.         if(itm<arr[md])
  26.         {
  27.             en=md-1;
  28.  
  29.         }
  30.         else
  31.         {
  32.                 bg=md+1;
  33.         }
  34.     md=int((bg+en)/2);
  35.     }
  36.  
  37.     if(arr[md]==itm)
  38.     {
  39.         loc=md;
  40.         cout<<"Iteam Found in "<<loc<<" number location";
  41.     }
  42.     else
  43.     {
  44.         cout<<"Iteam Not Found";
  45.  
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement