Advertisement
STANAANDREY

bin_search

May 29th, 2019
257
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.  
  4.  
  5. int main()
  6. {
  7. int v[100],n,i,aux,j,x;
  8. cout<<"n=";cin>>n;
  9. cout<<"x=";cin>>x;
  10. for (i=0;i<n;i++)
  11.  cin>>v[i];
  12.  
  13. for (i=0;i<n-1;i++)
  14.     for (j=i+1;j<n;j++)
  15.     if (v[j]<v[i])
  16. {
  17.     aux=v[j];
  18.     v[j]=v[i];
  19.     v[i]=aux;
  20. }
  21. int li=0,ls=n-1,mij,ok=0;
  22.  
  23. while (li<=ls&&!ok)
  24. {
  25.  mij=(li+ls)/2;
  26.  if (x==v[mij])
  27.     {
  28.         cout<<"EXISTA";
  29.         ok=1;
  30.     }
  31.     else
  32.        if (x<v[mij])
  33.        ls=mij-1;
  34.     else
  35.        li=mij+1;
  36. }
  37. if (!ok)
  38.     cout<<"NU EXISTA";
  39.  
  40. return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement