Advertisement
Mdimit

Binsearch(TODO)

Oct 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3.  
  4. using namespace std;
  5.  
  6. int binsearch(int* array, int x, int right, int left = 0){
  7.     int middle = (left + right) / 2;
  8.     if (*(array + middle) == x) return middle;
  9.     if (middle == left || middle == right) return -1;
  10.     if (*(array + middle) < x) binsearch(array, x, right, middle); else binsearch(array, x, middle);  
  11.     }
  12.  
  13. int main(){
  14.     int arr [7] = {1, 2, 3, 4, 5, 6, 7};
  15.     int * arr_ptr = &arr;
  16.     cout << binsearch(*arr_ptr, 2, 7)
  17.     return 0;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement