/***ITERATIVE VERSION*****/
int binarySearch(int [] a, int x){
//set limits and midpoint.
int low = 0;
int high = a.length-1;
int mid;
while(low <= high){//too small
mid(low+high) / 2;
if(a[mid] < x){
low = mid + 1;
}else if(a[mid] > x){//too large
high = mid - 1;
}else{//FOUND!!
return mid;
}
}
return -1; //Error
}
/***RECURSIVE VERSION*****/
int binarySearchRecursive(int[] a, int x, int low, int high) {
if(low > high) return -1; // Error
int mid = (low + high) / 2;
if(a[mid] < x){//too small
return binarySearchRecursive(a, x, mid+1, high);
}else if(a[mid] > x){//too large
return binarySearchRecursive(a, x, low, mid-1);
}else{//FOUND!!
return mid;
}
}