int find2(int * keys, int target, int len){
int high = len;
int low = -1;
int probe;
while (high - low > 1){
probe = (low + high) >> 1;
// does the following line improve the code?
if (keys[probe] == target) { return probe;}
if (keys[probe] > target) {
high = probe;
} else {
low = probe;
}
}
if (low == -1 || keys[low] != target) {
return -1;
} else {
return low;
}
}
Is binary search of an array faster with or without the check?
(From the very fine book: Beautiful Code)
The office hours are Sun/Tue/Thu 8-11 PM at Hillhouse 17 Rm 111. (Hannah will be there 7-10pm on Thursday)