Question
In C, given this implementation of bsearch, answer the following: a) After reading how nearly all binary searches are broken (see link below), does this
In C, given this implementation of bsearch, answer the following:
a) After reading how nearly all binary searches are broken (see link below), does this bsearch exibit this type of overflow problem, assuming all paramters passed are valid?
Justify why or why not.
link: https://research.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html
b) If void *arr is the base address of the array, and void *found is the address of the matching element in the array, given a nonzero size_t width, what explicit expression in C will convert "found" into its corresponding array index?
I know its going to be something along the lines of found-arr/width, but I am not certain of how to cast the variables and need help with the explicit statement.
c) Although most bsearches, this one included, assumes that the array passed in is sorted in ascending order, would passing in an unsorted array, or a reverse sorted array result in a crash/halted program, a false positive, a false negative? Explain at least the three listed cases for both unsorted and reverse sorted arrays passed into this bsearch.
void *apple_bsearch(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; int sign = compar(key, p); if (sign 0) return P; if (sign > 0) key > p: move right * base = (char *)p + width; nremain--; /* else move left * return NULL void *apple_bsearch(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; int sign = compar(key, p); if (sign 0) return P; if (sign > 0) key > p: move right * base = (char *)p + width; nremain--; /* else move left * return NULLStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started