MouseyN1

Cautare binara recursiv/iterativ

Sep 30th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. int caut_bin(int v[100], int st, int dr, int x) // recursiv
  2. {
  3.     if(st <= dr)
  4.     {
  5.         int mij;
  6.         mij = (st + dr) / 2;
  7.         if(x == v[mij])
  8.             return 1;
  9.         else if(x < v[mij])
  10.             caut_bin(v, st, mij - 1, x);
  11.         else caut_bin(v, mij + 1, dr, x);
  12.     }
  13.     else return 0;
  14. }
  15.  
  16. int caut_bin(int v[100], int st, int dr, int x) // iterativ
  17. {
  18.     int mij;
  19.     while(st <= dr)
  20.     {
  21.         mij = (st + dr) / 2;
  22.         if(v[mij] == x)
  23.             return 1;
  24.         else
  25.             if(x > v[mij])
  26.                 st = mij + 1;
  27.             else
  28.                 dr = mij - 1;
  29.     }
  30.     return 0;
  31. }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment