cupsamada

cautare binara consec

Feb 20th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. ///pentru catarea bina la numere crescatoare
  2. #include<iostream>
  3. using namespace std;
  4. int v[1000];
  5. int main()
  6. {
  7. int st,dr,poz,n,m,x;
  8. ///n e numarul de numere
  9. ///x e numarul ce trebuie cautat
  10. cin>>n>>x;
  11. for(int i=1; i<=n; i++)
  12. {
  13. cin>>v[i];
  14. }
  15. ///citesc vectorul
  16. st=1;
  17. dr=n;
  18. poz=0;
  19. ///atribui valori la dreapta ,stanga,pozitie
  20. while(st < dr && poz == 0)
  21. {
  22. m=(st+dr)/2;
  23. if(v[m]==x)
  24. poz=m;
  25. else
  26. if (v[m]<x)
  27. {
  28. st=m+1;
  29. }
  30. else
  31. {
  32. dr=m-1;
  33. }
  34. }
  35. cout<<poz;
  36. }
Add Comment
Please, Sign In to add comment