last day (15 days later) » 

17:29
0
Q: Number of guesses binary search would take to reach number

Gigi Bayte 2Essentially, given a start of an inclusive integer range $s$, an end of the range $f$ such that $s \le f$, and an integer $n$ such that $s \le n \le f$, how many guesses would binary search take to find $n$? For example, let $s = 1$, $f = 20$, and $n = 9$. Then, binary search would take $5$ guess...

You need $\lceil \log_2(f-s) \rceil$ guesses to guarantee to find $n.$ See: en.wikipedia.org/wiki/Binary_search_algorithm#Performance
@AdamRubinson I know that there would be $\lfloor \log_2 (f - s + 1) \rfloor + 1$ guesses maximum for a given range integer range $s$ to $f$. However, I meant on an individual basis. Like for $s = 1$, $f = 20$, and $n = 10$, it would take only 1 guess using binary search.
@GigiBayte2, please update your question to reflect the information in your last comment. It looks like you are looking for the AVERAGE number of probes not just the number? The following link discusses the average number of probes: en.wikipedia.org/wiki/Binary_search_algorithm
@NoChance My question appear to be up to date. No, I'm not looking for the average. I'm looking for a function that works for a given range start, range end, and target number as mentioned in the examples. So, it would essentially replace the pseudocode with a mathematical function that is able to compute $g$ from $s$, $f$, and $n$ (as opposed to a programmatic function).
You aren't saying anything about the input, so there can't be a formula. The sorted input arrays $[1,1,1,1,5]$ and $[1,5,5,5,5]$ will need a different number of probes for the same $s$, $f$ and $n$.
17:29
Just to clarify what the int(...) is doing, you're setting $m = \lfloor \frac{s + f}{2}\rfloor$, right? In that case, one reduction is $(s, f, n) \mapsto (0, f-s, n-s)$. Now in the case where $f = 2^r-1$ (and $s = 0$), I think the $k$th $m$ will agree with $f$ for the first $k-1$ bits and have $0$s afterwards, so the answer should be something like $r - (\text{trailing zeros in } n)$.
I expect if $f - s + 1$ is not a power of two you will not get a nice formula, and only a recursion that is equivalent to just simulating the process.
@RandyMarsh There is no array in question. In the case of $s = 1$ and $f = 20$, we are performing binary search for $n$ in $1, 2, \ldots, 20$. So, if you want to think of it as an array, you can think of the array as the one created by starting at $s$ and increasing by 1 until you get to $f$ if that makes sense.
@ronno Yes, that is correct. The only reason why I used int(...) instead of floor(...) was to do truncation that rounds towards 0 as opposed to floor that rounds towards -\infty. So, if \frac{s + f}{2} happened to be negative, then it would act as ceil instead. I am fine with the use of floor to int if it works out better mathematically, though.
@ronno Yep, that reduction makes complete sense. A reduction could also be made if $f$ and $n$ are still nonpositive after that reduction by doing $(0, f, n) \mapsto (0, -f, -n)$ (to account for truncation instead of floor). It is unfortunate that the recursion probably wouldn't lead to a nice solution. Thanks for looking into it!
To be clear, I haven't really looked for patterns when $f \ne 2^r - 1$ but it just seems much more complicated
@GigiBayte2 What do you mean by formula? I suspect that no formula exists, but I cannot prove this unless I know what the complete set of allowed formulae is.
@ronno Right, I agree.
@MikeEarnest I am just looking for some closed-form mathematical expression, I suppose. Similar to how there is a closed form expression for the Fibonacci sequence's nth term.
@GigiBayte2 There has to be an array as input or some other structure that records order. Otherwise we're not talking about a binary search. If your input is the integer interval $[1,n]$, then what is exactly the motivation of doing a binary search, since you can detect whether $i\in\mathbb N$ is in it in constant time?
 
1 hour later…
18:42
@RandyMarsh It is more of a matter of interest/curiosity/fascination rather than a matter of use in a practical application.

last day (15 days later) »