CosminVarlan

Cautare Binara Video Youtube

Jan 27th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. // vezi filmuletul: https://www.youtube.com/watch?v=hA5xtJFp8_c&feature=youtu.be
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int cauta(int v[], int n, int x){
  6.     int st=0;
  7.     int dr=n-1;
  8.     while(st<=dr)
  9.     {
  10.         int mid = (st+dr)/2;
  11.         if (v[mid]==x) return mid;
  12.         if (v[mid] < x) st = mid+1;
  13.         else dr = mid-1;
  14.     }
  15.     return -1;
  16. }
  17.  
  18. int main()
  19. {
  20.     int sir[13] = {1,3,5,6,7,9,10,12,13,15,18,19,20};
  21.     for(int i=1; i<=20; i++)
  22.         cout << i << " - "<< cauta(sir, 13, i) << endl;
  23.     return 0;
  24. }
Add Comment
Please, Sign In to add comment