text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
when we tore the phone book in half.
497.42
1.5
You can throw half of that phone book away as well.
498.92
2.72
So thank you very much for finding us the number 50.
501.64
2.25
So this is something that we did see, indeed, in week 0.
503.89
2.91
And it's what we would generally call binary search.
506.8
2.52
Or more generally, just a divide and conquer algorithm.
509.32
3.2
But today, what we start to do is to assess
512.52
2.289
just how much better this actually is.
514.809
2.291
And how we can go about finding things more programmatically.
517.1
3.85
So what Derek could have tried is what someone would generally
520.95
4.45
call linear search.
525.4
1.65
And as the name suggests, linear, meaning line,
527.05
2.07
you can just kind of solve a problem from left to right.
529.12
2.42
And indeed, while Derek did take a random approach,
531.54
2.58
it would have been functionally equivalent
534.12
1.87
for him simply to go left to right or right to left and find the number 50.
535.99
4.53
It's indeed there.
540.52
0.947
So let's introduce some pseudocode for this.
541.467
1.833
And there is no one right way to implement pseudocode.
543.3
3.29
Here is one of the most succinct ways I could come up
546.59
2.35
with for expressing this kind of left to right approach.
548.94
2.333
Or equivalently, right to left.
551.273
1.507
For each element in the array, if element you're looking for return true.
552.78
5.41
And so this is a very common paradigm in programming.
558.19
2.61
Just say something generally, like for each element in an array.
560.8
2.667
And the implication is that you would generally go left to right,
563.467
2.708
but it's equivalent to go right to left.
566.175
1.675
And if we indeed see at some location in that array
567.85
4.01
that array of boxes, the element we're looking for, we can just return true.
571.86
4.27
And yet, here I have return false.
576.13
2.77
But the indentation, so to speak, is very different.
578.9
2.92
Why have I aligned return false with the for loop
581.82
3.34
here all the way on the left as opposed to it being aligned in the middle
585.16
3.88
or one line deeper?
589.04
3.73
Yeah.
592.77
0.785
AUDIENCE: [INAUDIBLE].
593.555
0.916
SPEAKER 1: Yeah.
597.314
0.666
In order to denote that all of the indented elements
597.98
1.96
are within the for loop.
599.94
1
So it doesn't strictly matter how much you indent.
600.94
2.647
And indeed, in some languages, C among them,
603.587
1.833
it doesn't matter that you indent at all.
605.42
2.48
But for the sake of good style and readability,
607.9
2.84
we've indented this to convey to the human
610.74
2.1
especially that this "if" is only applicable when you're inside
612.84
4.27
of the for loop.
617.11
0.69
This return is only applicable when you're inside of this "if" condition.
617.8
3.27
And this return false really only happens
621.07
3.1
as the very last step in this program if nothing else actually returns a value.
624.17
4.95
So only if return true happens does this program say,
629.12
4.18
yes, I found the element you're looking for.
633.3
2.2
And you can think of this, therefore, as the sort of default case.
635.5
2.81
Now, just to toss it out there, why would this be wrong?
638.31
5.17
Let me go ahead and temporarily change this program
643.48
4.74
to be a more familiar if/else block.
648.22
2.87
So why is this seemingly logically fine?
651.09
3.93
It's a valid puzzle piece if you will, a la scratch.
655.02
3.23
But this program is now incorrect.
658.25
1.525
Why?
659.775
2.185
Yeah.
661.96
0.762
AUDIENCE: [INAUDIBLE].
662.722
0.916
SPEAKER 1: Once you hit it, it's automatically
669.004
1.916
going to come out of the for loop when?
670.92
1.2
Exactly.
672.12
0.499
When will this come out of the for loop?
672.619
2.113
AUDIENCE: [INAUDIBLE].
674.732
1.942
SPEAKER 1: Good.
676.674
0.666
So it's going to return false if the first element isn't
677.34
3.09
what you're looking for.
680.43
1
In so far as for each element in an array
681.43
1.939
kind of implies that you should be looking from left to right
683.369
2.541
or right to left, the first question you're
685.91
2.02
asking yourself when looking in that first element
687.93
2.1
is, is this the element you're looking for?
690.03
2.25
If it is, great.
692.28
0.667
You're going to return true.
692.947
1.166
And your answer is correct.
694.113
1.217
But if it's not the element you're looking
695.33
1.8
for, you're executing else return false immediately saying no, the element
697.13
4.19
I'm looking for is not here.
701.32
1.67
But when really the only question you've asked is, is the element
702.99
3.08
I'm looking for the very first in the array?
706.07
2.74
And so, indeed, this would just be logically incorrect
708.81
2.25
because you might as well not look at any of the array
711.06
2.25
if you're only looking at that very first element.
713.31
2.88
So not what we intended.
716.19
1.29
So indeed, that original pseudocode is just fine.
717.48
3.22
So alternatively, there's something called binary search, which
720.7
3.03
Derek actually used intuitively for his second approach,
723.73
3.73
where he was told in advance that the array is sorted numerically from left
727.46
4.24
to right.
731.7
0.5
This gives him more information, much like I had in week 0
732.2
2.416
when I searched a phone book that I knew was alphabetical.
734.616
3.134
And you can write this algorithm in any number of ways, too.
737.75
2.54
I kind of borrowed the spirit of our Mike Smith algorithm from week 0
740.29
3.947
and I proposed this.
744.237
0.833
Look at the middle of the array.
745.07
1.87
If element you're looking for return true.
746.94
2.09
We just get lucky if it's smack dab in the middle of the array.
749.03
2.68
Else if the element is to the left, because the element
751.71
3.99