You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: binary_search/README.markdown
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,7 @@ The important pattern to notice is that the number of possible correct answers (
39
39
40
40
Notice that in the second example, however, we didn't even need to use a third guess, because there was only one possible answer after the second guess. Three guesses is the *slowest* it would take to find the answer in a set of eight items, not the fastest. This is still potentially much faster than a worst-case scenario if we used a *linear* search instead of a *binary* search. If we used a linear search, it would have taken us 8 guesses to guess the number 8 in the first example if we started guessing at 1, because our next guess would have been 2, then 3, and so on.
41
41
42
-
Binary search is also sometimes called *logarithmic search* because the search speed is calculatable by taking a logarithm of the length of the list. A logarithm is the opposite of an exponent. 2 raised to the power of 3 (that is, 2 multiplied by itself 3 times) equals eight (2<sup>3</sup> = 8). We can retrieve the exponent (the 3, which is the most guesses we'd need to search an 8-item list) by applying the logarithm function to the number 8 (the size of our search space) with a base of 2 (because we're halving the space at each guess): log<sub>2</sub>8 = 3.
42
+
Binary search is also sometimes called *logarithmic search* because the search speed is calculatable by taking a logarithm of the length of the list. [A logarithm is the opposite of an exponent](https://www.khanacademy.org/math/algebra-home/alg-exp-and-log/alg-graphs-of-logarithmic-functions/v/comparing-exponential-logarithmic-functions). 2 raised to the power of 3 (that is, 2 multiplied by itself 3 times) equals eight (2<sup>3</sup> = 8). We can retrieve the exponent (the 3, which is the most guesses we'd need to search an 8-item list) by applying the logarithm function to the number 8 (the size of our search space) with a base of 2 (because we're halving the space at each guess): log<sub>2</sub>8 = 3.
43
43
44
44
Using logarithmic math, you can easily find out how many guesses it would take you to find a given item in an arbitrarily large search space. For instance, if you had to guess a number between 1 and 100, it would take you at most log<sub>2</sub>100 guesses if you always halved the search space at each guess (i.e., if you "used binary search").
0 commit comments