I have a functioning binary array search algorithm, here it is:
int binarySearch(int array[],int n,int elem){
int high =n-1;
int low = 0;
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(elem>array[mid]){
low=mid+1;
}
else if(elem<array[mid]){
high=mid-1;
}
else return mid;
}
return -1;
}
but now I want to know the amount of comparisons made between elements of the array, I know how to do that, but my questions are:
When the compiler finds one of the clauses to be true, does it still go into the other clauses? example: when elem>array[mid] it makes one comparison, so now he should know that the following "else if" is false, but does it still make the comparison?
And what about the "else"? when he found the "if" and "else if" to be false, does he secretly make a comparison (elem==array[mid])?
Thanks!
Aucun commentaire:
Enregistrer un commentaire