int arrayBinSearchIter(int* arr,int val,int len) {
int min = 0;
int max = len - 1;
int curr = -1;
while(min <= max) {
curr = (min+max)/2;
if(arr[curr] == val) return(curr);
if(arr[curr] < val) min = curr + 1;
if(arr[curr] > val) max = curr - 1;
}
return(-1);
}
int arrayBinSearchRec(int* arr,int val,int min,int max) {
if(min > max) return(-1);
int curr = (min+max)/2;
if(arr[curr] == val) return curr;
if(arr[curr] < val) min = curr + 1;
if(arr[curr] > val) max = curr - 1;
return arrayBinSearchRec(arr,val,min,max);
}
No comments:
Post a Comment