我对以下来自Apple的二进制搜索方法有疑问。
void *bsearch_from_a(const void *key, const void *base, size_t nmemb,
size_t width, int (*compar)(const void *, const void *)) {
for (size_t nremain = nmemb; nremain != 0; nremain >>= 1) {
void *p = (char *)base + (nremain >> 1) * width; // I don't get this
// this is comparing key & where currently p is pointing
int sign = compar(key, p);
if (sign == 0) { // if p and key are equal, sign is 0
return p;
}
if (sign > 0) { // when key > p, move right
base = (char *)p + width;
nremain--;
}
}
return NULL;
}
当完成上述for循环并且在数组中未找到键时,有没有办法找到基数右边(包括基数)的元素数量?您可以创建所需的任何变量。任何帮助将不胜感激。预先感谢。