text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
And what's happening here?
3,566.08
1.29
This is like me walking across the audience again and again
3,567.37
4
and again and again.
3,571.37
1.27
You don't see any of this bubbling effect,
3,572.64
2.02
but you do see the smallest element being selected again
3,574.66
4.03
and again and again.
3,578.69
1.59
So we're kind of cleaning up that mess one step at a time.
3,580.28
3.65
It's still going to take some steps, but you can perhaps
3,583.93
2.35
see it a little better and a little faster here.
3,586.28
2.42
And spoiler alert, when we skip forward to the end, that's what it looks like.
3,588.7
3.95
If we now randomize it one last time and do insertion sort,
3,592.65
4.35
this one kind of feels and looks different.
3,597
3.97
It's kind of mostly fixing things, but then it
3,600.97
2.487
kind of goes in and fills in the gaps.
3,603.457
1.583
Because indeed, it's moving forward one at a time, grabbing the element,
3,605.04
3.35
and then shifting the element as needed to make room for whatever number
3,608.39
3.22
it's encountered.
3,611.61
1.22
So that's why sometimes it only takes a few steps
3,612.83
2.054
because you find where it belongs.
3,614.884
1.416
That time it took a while to find the space that it belongs.
3,616.3
4.3
But generally, the list is getting more and more sorted.
3,620.6
2.71
And once we do get to the very end, this one
3,623.31
2.81
we can actually feel being a little faster.
3,626.12
1.98
Which isn't necessarily a testament to it being fundamentally better.
3,628.1
2.874
All of these remain on the order of n squared steps.
3,630.974
3.296
And now indeed, it's starting to drag, kind of like our blue books.
3,634.27
2.83
And now we're actually complete.
3,637.1
1.8
But what if we were to compare these things?
3,638.9
3.19
Bubble sort on the left, selection sort in the middle,
3,642.09
2.63
and then a third algorithm that we've not seen this time called merge sort.
3,644.72
4.06
So insertion sort is roughly the same as all of these.
3,648.78
2.39
So this visualization lets us try three.
3,651.17
2.295
To start this, I have to click on them.
3,653.465
1.625
So there's a little bit of a fudge factor
3,655.09
1.708
here where I have to click on all three programs quickly in succession
3,656.798
2.972
to see them.
3,659.77
0.72
But let's see if we can't do fundamentally better.
3,660.49
3.63
Merge sort is our new friend on the right.
3,664.12
4.78
Oh my god.
3,668.9
0.65
And these are different visualizations, but they're the same algorithms.
3,672.97
3.9
And it's not that they're just poorly implemented.
3,676.87
2.14
It's arguably that they're poorly designed.
3,679.01
2.49
They are all correct, but merge sort clearly
3,681.5
3.81
seems to have some upsides over selection sort.
3,685.31
2.307
And if you'll take it on faith for now, just because we can only fit three
3,687.617
3.083
on the screen, insertion sort, too.
3,690.7
2.39
You see the bigger elements bubbling up to the top.
3,693.09
2.21
You see us selecting.
3,695.3
1.78
Again, it turns out this person implemented selection sort
3,697.08
3.285
by selecting the biggest element.
3,700.365
1.375
But that's fundamentally equivalent to selecting the smallest element.
3,701.74
2.916
And that's because you can see them all stacking here in this way.
3,704.656
3.594
But my god, I wonder, can me start this again?
3,708.25
4.26
Let's try.
3,712.51
0.5
Let's see if we can lap it.
3,713.01
1.11
Come on.
3,714.12
0.29
Come on.
3,714.41
0.5
Come on.
3,714.91
0.93
Come on.
3,715.84
2.27
Oh, so close.
3,718.11
0.96
All right.
3,719.07
0.5
Had I not talked as much.
3,719.57
1.041
So merge sort.
3,720.611
0.759
Where is this magical algorithm?
3,721.37
2.08
What are we actually doing wasting time on these other things?
3,723.45
3.96
Well, let's consider exactly that, but we need sort of a secret ingredient.
3,727.41
3.86
And let me revisit Mike Smith for just a moment.
3,731.27
2.292
So you'll recall, this was the pseudocode
3,733.562
1.708
that we had from Mike Smith in week 0.
3,735.27
2.17
And we decided when Mike is earlier in the book
3,737.44
3.98
or when Mike is later in the book, to just go to an earlier step.
3,741.42
3.87
Thereby, inducing some kind of loop.
3,745.29
2.27
But suppose we changed that language to be this instead.
3,747.56
3.18
Don't go to a specific line number.
3,750.74
1.85
Don't go to some earlier step.
3,752.59
2.21
But just more generally, say, search for Mike
3,754.8
2.46
in the left half of the book or search for Mike in the right half of the book.
3,757.26
3.25
This is borrowing an idea that I proposed a moment ago when
3,760.51
3.45
we were searching for something else.
3,763.96
2.28
And what word did we ascribe to this kind
3,766.24
2.14
of approach of this cyclical use of your own definition?
3,768.38
5.5
Yeah.
3,773.88
0.69
AUDIENCE: [INAUDIBLE].
3,774.57
0.38
SPEAKER 1: Recursion.
3,774.95
0.99
So recursion is actually this secret ingredient
3,775.94
2.86
that we can leverage, both intuitively and in code,
3,778.8
2.75
to implement this new and improved fourth algorithm, merge sort.
3,781.55
4.27
And indeed, this we'll use demonstrative of a class of algorithms
3,785.82
3.117
that doesn't behave in quite the same way fundamentally as selection
3,788.937
2.833
sort, insertion sort, and bubble sort.
3,791.77
1.583
Indeed, you can do better than big O of n squared.
3,793.353
2.477
You can do much better, as you felt a moment ago with visualization.
3,795.83
3.76
And merge sort is one such algorithm with which we can do that.
3,799.59
3.25
And amazingly, the pseudocode code is this simple.
3,802.84
3.16
When you're given n elements, if n is less than 2, just return.
3,806
4.17
If you have fewer than 2 elements, the list is of size 1, which is sorted,
3,810.17
3.44
or size 0, which is also meaninglessly sorted.
3,813.61
2.65
So you're done.
3,816.26
1.199
So that's kind of a base case, like a default scenario.
3,817.459
2.291
Let's just make sure that no matter what, we eventually
3,819.75
2.291