/** * ch-ibs.c * An implementation of binary search by C.H. */ #include "ibs.h" int kernel(int* arr, int start, int end, int key); int ibs (int* arr, int length, int key) { //call the kernel return kernel (arr, 0, length - 1, key); } // ibs int kernel (int* arr, int start, int end, int key) { //if the target is not in the array if (start > end) return -1; // int mid = (start + end) / 2; int mid = start + (end-start)/2; // NOT int mid = start/2 + end/2; //if the mid is our key if(arr[mid] == key) return mid; //if in the first half else if (arr[mid] > key) { return kernel(arr, start, mid-1, key); } //if in the second half else{ return kernel(arr, mid+1, end, key); } }