|
1 | 1 | # Binary Search
|
2 | 2 |
|
3 |
| -- Binary search is a divide-and-conquer algorithm; its input is a **sorted** list of elements. If an element you're looking for is in that list, binary search returns the position **where it's located**. Otherwise, binary search returns **null**. |
| 3 | +- Binary search is an example of the famous divide-and-conquer algorithm; its input is a **sorted** list of elements. If an element you're looking for is in that list, binary search returns the position of the item. Otherwise, it returns **null**. |
4 | 4 |
|
5 |
| -- We can only conduct binary search with ordered lists, such as alphabetically ordered phone list, ordered list of numbers etc. |
| 5 | +- You can only conduct binary search in ordered lists. |
6 | 6 |
|
7 |
| -- In general, for any list of n, binary search will take **log<sub>2</sub>n** steps to run in the worst case, whereas simple search will take **n** steps. |
| 7 | +- In general, for any list of `n`, binary search will take **log<sub>2</sub>n** steps to run in the worst case, whereas simple search will take **n** steps. |
8 | 8 |
|
9 | 9 | 
|
10 | 10 |
|
11 |
| -In this book, when I talk about running time in Big O notation (explained a little later), log always means log<sub>2</sub>. |
| 11 | +In the documents, log always means log<sub>2</sub>. |
12 | 12 |
|
13 | 13 | ## Running Time
|
14 | 14 |
|
15 | 15 | | Running Time | Log(n) vs n |
|
16 | 16 | | ------------ | ----------- |
|
17 | 17 | |  |  |
|
18 | 18 |
|
19 |
| -The key idea is that when binary search makes an incorrect guess, the portion of the array that contains reasonable guesses is reduced by at least half. If the reasonable portion had 32 elements, then an incorrect guess cuts it down to have at most 16. Binary search halves the size of the reasonable portion upon every incorrect guess. |
| 19 | +When binary search makes an incorrect guess, the portion of the array that contains reasonable guesses is reduced by at least half. If the reasonable portion had 32 elements, then an incorrect guess cuts it down to have at most 16. Binary search halves the size of the reasonable portion upon every incorrect guess. |
| 20 | + |
| 21 | +## Interesting Fact |
20 | 22 |
|
21 | 23 | > According to 'Programming Pearls', only 10% of professional programmers are able to implement binary search in their code. They can explain it very well, but coding it is a challenge for them.
|
0 commit comments