#define DEBUG_TEST 1

#include <stdio.h>

int binary_search_rec_helper(int* A, int start, int end, int item) {
  if (start>end)
    return -1; // item not found

  int mid = (start+end)/2;

  if (A[mid] == item)
    return mid;
  else if (A[mid] > item)
    binary_search_rec_helper(A, start, mid-1, item);
  else // A[mid] < item
    binary_search_rec_helper(A, mid+1, end, item);
}

int binary_search_rec(int* A, int n, int item) {
   return binary_search_rec_helper(A, 0, n-1, item);
}


// idea: say A is the entire array of length n
// if we want to search in A[start...end], we send (A+start,n=end-start+1)
int binary_search_rec2(int* A, int n, int item) {

  // stopping conditions
  if (n == 0)
    return -1;

  int mid = n/2;

  // another stopping conditions
  if (A[mid]==item)
    return mid;

  // recursive calls
  if (A[mid] > item) { // if item < A[mid] return the index
    int i = binary_search_rec2(A, mid, item);
    if (i == -1) // item not found
      return -1;
    else  // return the index
      return i;
  }
  else  { // need to search in the right half:  A[mid+1...n-1]
    int i = binary_search_rec2(A+mid+1, n-mid-1, item);
    if (i == -1)  // item not found
      return -1;
    else     // i is the relative index, need to adjust to the absolute index in A
      return mid+1+i;
  }
}



int main() {
  int arr[] = {1,2,4,5,7,10,15,20};
  int item = 10;
  int ind = binary_search_rec(arr,8,item);
  if (ind>=0)
    printf("%d found in A[%d]\n", item, ind);
  else
    printf("item not found\n");
  return 0;
}
