Advertisement
STANAANDREY

bin_src rec

Aug 9th, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int rec_binarySearch(int arr[], int st, int dr, int x)
  5. {
  6.     if (st <= dr)
  7.     {
  8.         int m = (st + dr)/2;
  9.         if (x == arr[m])
  10.             return m;
  11.         if (x < arr[m])
  12.             return rec_binarySearch(arr, st, m - 1, x);
  13.         return rec_binarySearch(arr, m + 1, dr, x);
  14.     }
  15.     return -1;
  16. }
  17.  
  18. int main(void)
  19. {
  20.     int arr[] = {2, 3, 4, 10, 40};
  21.     int n = sizeof(arr)/ sizeof(int);
  22.     int x = 10;
  23.     int res = rec_binarySearch(arr, 0, n - 1, x);
  24.     if (res != -1)
  25.         cout<<"Index: " << res;
  26.     else
  27.         cout<<"Element inexistent!";
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement