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