text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
quit, even if the list is super-small.
3,822.041
2.399
But then, the magic is hidden in those last three lines.
3,824.44
2.84
Else, sort the left half of the elements,
3,827.28
2.93
then sort the right half of the elements,
3,830.21
1.76
which is completely punting so to speak.
3,831.97
2.13
Just kind of recursively calling yourself.
3,834.1
2.04
But the key detail here is that then you have to merge the sorted halves.
3,836.14
4.72
You're not just looking for Mike to the left and looking for Mike to the right.
3,840.86
3.312
You're doing something on the left, something on the right,
3,844.172
2.458
sorting each half, but then you have to kind of weave things together.
3,846.63
3.43
And we'll see how expensive that actually is.
3,850.06
2.85
So let's take an example.
3,852.91
1.3
And I mocked this up with some very simple animation so we
3,854.21
3.82
could walk through it step-by-step.
3,858.03
1.585
But consider the following.
3,859.615
1.125
So here is that same list from before where
3,860.74
1.791
we had a whole bunch of volunteers.
3,862.531
1.699
But rather than call up humans, we'll just do this visually.
3,864.23
2.58
Let me consider the fact that this is really just an array.
3,866.81
3.245
So I'm just going to box everything like this.
3,870.055
2.015
And this is actually key that we have random access to these elements.
3,872.07
3.28
We can jump right to the elements we want
3,875.35
1.82
and code ultimately, too, if we cared.
3,877.17
2.16
And now, let's focus on the left-hand side.
3,879.33
2.35
So I've wrapped 4, 8, 6, 2 in a solid line
3,881.68
4.682
and left everything else dash because I want to focus
3,886.362
2.208
our attention on sorting the left half.
3,888.57
2.23
So again, to rewind this is the algorithm.
3,890.8
2.51
And the goal at hand is to walk through visually
3,893.31
2
an example whereby we sort 8 elements.
3,895.31
2.684
And we do that by sorting the left half, sorting the right half,
3,897.994
2.666
and then somehow merging these things together.
3,900.66
3.16
So how do we do this?
3,903.82
1.14
Well, let's consider the left half.
3,904.96
2.23
All right.
3,907.19
0.95
How do I sort a list of 4 elements?
3,908.14
2.07
What does it mean to sort the left half?
3,910.21
1.78
Well, what algorithms do I have in my toolkit for sorting elements?
3,911.99
2.9
Yeah, selection sort, insertion sort, bubble sort, but-- merge sort.
3,917.64
4.68
And that's where this gets weird quickly.
3,922.32
2.29
You want to use your same algorithm to sort the left-hand side of this list.
3,924.61
4.324
And yet, we haven't even finished defining this algorithm,
3,928.934
2.416
but that's going to be OK we'll see.
3,931.35
2.13
So let's sort the left half.
3,933.48
1.19
How do you sort the left half of a list?
3,934.67
1.666
Well, you sort a list of size 4.
3,936.336
1.494
What does that mean?
3,937.83
0.85
How do you sort a list of size 4?
3,938.68
2.66
Sort the left half of that.
3,941.34
2.19
So let's focus on 4 and 8.
3,943.53
2.19
How do you sort a list of size 2?
3,945.72
1.96
Merge sort it.
3,947.68
1.25
So how do you sort a list of size 2?
3,948.93
1.98
Sort the left half.
3,950.91
1.86
OK, beautifully.
3,952.77
1.16
Done.
3,953.93
1.72
Done.
3,955.65
1.29
Sort the right half.
3,956.94
1.51
Nice, done.
3,958.45
1.56
I mean, I'm doing real work, or claiming to.
3,960.01
2.57
But now I have to do something more.
3,962.58
1.69
Now I have to merge the two halves.
3,964.27
1.46
So I'm taking a step back.
3,965.73
1.33
I have two lists now, one of size 1, one of size 1.
3,967.06
3.579
I have to merge these things, two together.
3,970.639
1.791
Which comes first, 4 or 8?
3,972.43
2.69
4.
3,975.12
0.67
So let's create some extra space for it.
3,975.79
2.03
Let's put 4 here and let's put 8 here.
3,977.82
3.53
So here's a key detail with merge sort, kind
3,981.35
2.394
of cheating, at least at the moment, I'm using some extra space.
3,983.744
2.666
So I hope you won't mind.
3,986.41
1.23
To achieve this, I'm using more memory, more RAM,
3,987.64
2.76
that I wasn't allowing myself for selection sort, bubble
3,990.4
2.53
sort, or insertion sort.
3,992.93
1.011
OK.
3,993.941
0.499
So now it's going to be easy to get lost quickly.
3,994.44
2.041
So what have I just done?
3,996.481
1.239
I have sorted a list of size 2 by sorting the left half, done.
3,997.72
4.06
Right half, done.
4,001.78
1.27
Merging it together.
4,003.05
1.4
So now I have a sorted list of size 2.
4,004.45
3.69
Rewind the story.
4,008.14
0.87
What step comes next?
4,009.01
2.76
Sort the right half.
4,011.77
2.06
That was of size 2.
4,013.83
1.2
So if you kind of mentally buffer all of the to-dos that we're doing,
4,015.03
3.5
and then pluck them off as we go, you'll see where the story goes.
4,018.53
3.68
Now I want to sort the right half of the original list.
4,022.21
2.31
Sorry, the right half of the left half of the original list.
4,024.52
4.32
How do I do that?
4,028.84
0.94
Sort of left half, done.
4,029.78
1.7
Sort the right half, done.
4,031.48
1.77
Merge them together.
4,033.25
1.21
What comes first, 6 or 2?
4,034.46
1.86
OK.
4,036.32
0.5
So let's give ourselves a little more memory for that
4,036.82
1.76
so we have a place to put it.
4,038.58
1.33
2.
4,039.91
0.97
And then 6.
4,040.88
1.271