Friday, March 21, 2014

Binary Search

int bisearch(char** arr, int b,int e, char* v){

int minIdx=b,maxIdx=e,midIdx;

while(minIdx< maxIdx-1){

midIdx= minIdx+(maxIdx- minIdx)/2;

if(strcmp(arr[midIdx],v)<=0){             //end case for while:1. minI=maxI; 2.minI = maxI -1;

minIdx= midIdx;
}
else{
            maxIdx = midIdx;
}
}
if(!strcmp(arr[maxIdx],v){
return maxIdx;
}
else if(!strcmp(arr[minIdx],v){
return minIdx;
}
else{
return -1;
}
}

No comments:

Post a Comment