Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int rec_binarySearch(int arr[], int st, int dr, int x)
- {
- if (st <= dr)
- {
- int m = (st + dr)/2;
- if (x == arr[m])
- return m;
- if (x < arr[m])
- return rec_binarySearch(arr, st, m - 1, x);
- return rec_binarySearch(arr, m + 1, dr, x);
- }
- return -1;
- }
- int main(void)
- {
- int arr[] = {2, 3, 4, 10, 40};
- int n = sizeof(arr)/ sizeof(int);
- int x = 10;
- int res = rec_binarySearch(arr, 0, n - 1, x);
- if (res != -1)
- cout<<"Index: " << res;
- else
- cout<<"Element inexistent!";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement