Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
The algorithm Jon used, linear search, seemed familiar to us and you could understand it because it's how most of us search for things in real life anyway. Brittney's approach on the other hand got results quickly but was a bit harder to understand so let's break it down.

0:00
The algorithm John used, linear search, seemed familiar to us.

0:03
And you could understand it because it's how most of us search for

0:06
things in real life anyway.

0:08
Britney's approach, on the other hand,

0:10
got results quickly, but it was a bit harder to understand.

0:13
So let's break it down.

0:14
[SOUND] Just like John's approach, Britney started with a series of values, or

0:18
list of numbers, as her input.

0:20
Where John just started at the beginning of the list and searched sequentially,

0:25
Britney's strategy is to always start in the middle of the range.

0:28
From there, she asks a comparison question,

0:31
is the number in the middle of the range equal to the answer she's looking for?

0:36
And if it's not, is it greater than or less than the answer?

0:39
If it's greater than,

0:41
she can eliminate all the values less than the one she's currently evaluating.

0:46
If it's lesser than the answer,

0:47
she can eliminate all the values greater than the one she's currently evaluating.

0:52
With the range of values that she's left over with,

0:55
she repeats this process until she arrives at the answer.

0:58
[SOUND] Let's visualize how she did this by looking at Round 3.

1:03
[SOUND] In Round 3, the number of values in the range was 100.

1:06
The answer was 5.

1:07
The bar here represents the range of values, 1 on the left, 100 on

1:11
the right and this pointer represents the value Britney chooses to evaluate.

1:16
So she starts in the middle at 50, she asks is it equal to the answer?

1:21
I say it's too high.

1:22
So this tells her that the value she is evaluating is greater than our target

1:27
value.

1:27
Which means there's no point in searching any of the values to the right of 50,

1:32
that is values greater than 50 in this range.

1:35
So she can discard those values altogether.

1:38
[SOUND] She only has to consider values from 1 to 50 now.

1:42
[SOUND] The beauty of this strategy, and

1:44
the reason why Britney was able to find the answer in such few turns,

1:48
is that with every value she evaluates, she can discard half of the current range.

1:53
On her second turn, [SOUND] she picks the value in the middle of the current range,

1:57
which is 25.

1:58
She asks the same question.

2:00
I say that the value is too high again.

2:03
And this tells her that she can discard everything greater than 25.

2:07
[SOUND] And the range of value drops from 1 to 25.

2:10
Again, she evaluates the number in the middle, roughly.

2:13
So that'd be 13, here.

2:15
I tell her, this is still too high.

2:17
She discards the values greater, moves to value 7, which is still too high.

2:21
Then she moves to 4, which is now too low.

2:24
She can discard everything less than 4, which leaves the numbers 4 through 7.

2:29
Here, she picks 6, which is too high, which only leaves one value, 5.

2:34
This seems like a lot of work.

2:36
But being able to get rid of half the values with each turn

2:40
is what makes this algorithm much more efficient.

2:43
Now, there's one subtlety to using binary search, and

2:46
you might have caught on to this.

2:47
For this search method to work, as we've mentioned, the values need to be sorted.

2:52
[SOUND] With linear search, it doesn't matter if the values are sorted.

2:55
Since a linear search algorithm just progresses sequentially,

2:59
checking every element in the list.

3:02
If the target value exists in the list, it will be found.

3:05
Let's say this range of values, 1 to 100, was unsorted.

3:09
Britney would start at the middle with something like 14 and

3:12
ask if this value was too low or too high?

3:15
I say it's too high so she discards everything less than 14.

3:19
Now, this example starts to fall apart here, because well,

3:22
Brittany knows what numbers are less than 14 and greater than 1.

3:26
She doesn't need an actual range of values to solve this.

3:29
A computer, however, does need that.

3:31
Remember, search algorithms are run against lists containing all sorts

3:36
of data.

3:37
It's not always just a range of values containing numbers.

3:39
In a real use case of binary search, which we're going to implement in a bit.

3:44
The algorithm wouldn't return the target value because we already know

3:48
that it's a search algorithm.

3:50
So we're providing something to search for.

3:52
Instead, what it returns is the position in the list that the target occupies.

3:56
Without the list being sorted,

3:58
a binary search algorithm will discard all the values to the left of 14 which over

4:03
here could include the position where our target value is.

4:06
Eventually we get a result back saying the target value doesn't exist in the list

4:10
which is inaccurate.

4:12
Earlier when defining linear simple search I said that the input was a list of

4:17
values and the output was the target value or

4:19
more specifically the position of the target value in the list.

4:24
So at binary search there's also that precondition the input list must be

4:28
sorted.

4:29
So let's formally define binary search.

4:31
First the input a sorted list of values.

4:35
The output,

4:35
the position in the list of the target value we're searching for, [SOUND] or

4:40
some sort of values indicate that the target does not exist in the list.

4:44
Remember our guidelines for defining an algorithm?

4:47
Let me put those up again really quick.

4:48
The steps in the algorithm need to be in a specific order.

4:52
The steps also need to be very distinct.

4:54
The algorithms should produce a result and

4:57
finally the algorithms should complete in a finite amount of time.

5:01
Letâ€™s use those to define this algorithm.

5:04
Step one, [SOUND] we determine the middle position of the sorted list.

5:07
Step two, we compare the element in the middle position to the target element.

5:12
Step three, if the elements match, we return the middle position and end.

5:17
[SOUND] If they don't match, in step four, we check whether the element in

5:21
the middle position is smaller than the target element.

5:24
[SOUND] If it is, then we go back to step two, with a new list that goes from

5:29
the middle position of the current list to the end of the current list.

5:33
[SOUND] In step five, if the element in the middle position is greater than

5:37
the target element, then again [SOUND] we go back to step two.

5:40
With a new list that goes to the start of the current list to the middle

5:44
of the current list.

5:45
[SOUND] We repeat this process until the target element is found.

5:49
Or until a sublist contains only one element.

5:52
If that single element sublist does not match the target elements,

5:57
then we end the algorithm.

5:59
Indicating that the element does not exist in the list.

6:02
Okay, so

6:02
that is the magic behind how Britney managed to solve around much faster.

6:07
In the next video let's talk about the efficiency of binary search
You need to sign up for Treehouse in order to download course files.
Sign up