Skip to content

Commit 66811c1

Browse files
authored
Create Binary-Search.md
1 parent 9e0746e commit 66811c1

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

Notes/Binary-Search.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Binary Search
2+
3+
## Motivating Problem I
4+
5+
Imagine that you have a phone book and want to find your friend's phone number.
6+
7+
With so many people listed, how do you efficiently search for your friend's last name?
8+
9+
## Motivating Problem II
10+
11+
You ask your friend to think of a number between 1 and 100; you attempt to guess what number they're thinking of, and they will tell you if the number you guessed is bigger or smaller than their number.
12+
13+
You keep doing this until you successfully find their number; the number of guesses you had is your score, so you attempt to minimize your score.
14+
15+
Since there are only 100 numbers, your score should be relatively small, but what if you told your friend to think of a number between 1 and 1,000,000?
16+
17+
How can we quickly find what number they are thinking of?
18+
19+
## Definition
20+
21+
One of the fundamental algorithms in computer science, the *binary search* algorithm is used to quickly find a value in a sorted sequence.
22+
23+
The algorithm works by repeatedly dividing in half the portion of the sequence that could contain the target value, until you've narrowed down the possible locations to just one.
24+
25+
## Time Complexity
26+
27+
Suppose we have a sorted sequence of 32 numbers.
28+
29+
If we randomly guess, the maximum number of guesses that it will take to find the target number will be 32.
30+
31+
If we use binary search, the maximum number of guesses that it will take to find the target number will be 5.
32+
- Remember that every guess eliminates half of the remaining sequence
33+
34+
Now, suppose we have a sorted sequence of **_N_** numbers.
35+
36+
If we randomly guess, the maximum number of guesses that it will take to find the target number will be **_N_**.
37+
38+
If we use binary search, the maximum number of guesses that it will take to find the target number will be **_log(N)_**.
39+
40+
How much faster is **_log(N)_** than **_N_**?
41+
42+
| **_N_** | **_log(N)_** |
43+
| ------------- | ------------ |
44+
| 10 | ~3 |
45+
| 100 | ~7 |
46+
| 10,000 | ~13 |
47+
| 1,000,000 | ~20 |
48+
| 1,000,000,000 | ~30 |
49+
50+
## Pseudocode
51+
52+
Let's say that we want to code the binary search algorithm to find a target value in a sorted array of numbers.
53+
54+
We will use two variables, `lo` and `hi`, to represent the indices of the first and last element of the section of the array that we are currently looking at.
55+
56+
Let's call the index of the middle number in this target section `mid`, which can be found by finding the average of `lo` and `hi`.
57+
58+
There are three possible scenarios:
59+
60+
1. `array[mid] == targetValue`
61+
62+
If the middle number of our current sequence is equal to the target value, we have found what we are looking for and are done
63+
64+
2. `array[mid] < targetValue`
65+
66+
If the middle number of our sequence is less than the target value, then we know that the bottom half of the current sequence can be eliminated.
67+
68+
We can adjust the sequence by setting `lo = mid`
69+
70+
3. `array[mid] > targetValue`
71+
72+
If the middle number of our sequence is greater than the target value, then we know that the top half of the current sequence can be eliminated.
73+
74+
We can adjust the sequence by setting `hi = mid`
75+
76+
Now that we have the three possible scenarios handled, we just need to define when to terminate the loop if we never found our target value.
77+
78+
We want to keep searching while we have some range of numbers to check, so if `lo` ever becomes greater than `hi`, then our target value is not in the array due to the fact that we have run out of numbers to check.
79+
80+
```java
81+
int lo = 0;
82+
int hi = N-1;
83+
84+
while (lo <= hi) {
85+
mid = lo + (hi -lo) / 2;
86+
if (array[mid] == targetValue) break;
87+
if (array[mid] < targetValue) lo = mid;
88+
if (array[mid] > targetValue) hi = mid;
89+
}
90+
```
91+
92+
## Problems
93+
94+
- [Aggressive cows](spoj.com/problems/AGGRCOW)
95+
- [ABCDEF](spoj.com/problems/ABCDEF)
96+
- [Cutting Cheese](icpc.kattis.com/problems/cheese)

0 commit comments

Comments
 (0)