VasilM

algo1_2

Oct 14th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. // factoriel
  5. /*
  6. int fact (int n) {
  7.     if (n > 1) return n*fact(n-1);
  8.     return 1;
  9. }
  10. */
  11.  
  12. // dvoichno tyrsene
  13. int binSearch(int left, int right, int x, int array[]) {
  14.     int mid = (left + right) / 2;
  15.     if( array[mid] == x ) return mid;
  16.     else if( left == right ) return -1;
  17.     else {
  18.         if( x > mid ) binSearch (mid+1, right, x, array);
  19.         else binSearch(left, mid-1, x, array);
  20.     }
  21. }
  22.  
  23.  
  24. int main() {
  25.     int array[] = {1 ,2 ,3 ,4 ,5 ,6 ,7};
  26.     int x = 8;
  27.     int res = binSearch(0,7,x,array);
  28.     cout << res << endl;
  29.     system("pause");
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment