text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
And it looks like I
really cheated, though. | 4,255.72 | 1.88 |
I seemed to first say,
hey, I need some extra RAM. | 4,257.6 | 2.68 |
And I took it. | 4,260.28 | 0.94 |
And then I didn't
mention I need more RAM. | 4,261.22 | 1.83 |
I didn't mention I needed even more RAM. | 4,263.05 | 1.666 |
So this seems to have taken like four
times as much total memory than any | 4,264.716 | 3.863 |
of our previous algorithms, but
that's not actually the case. | 4,268.579 | 2.541 |
I left this here pictorially
so we could see the history. | 4,271.12 | 2.71 |
Technically, you do need more RAM to
implement this algorithm, merge sort. | 4,273.83 | 3.741 |
But we could have cut corners. | 4,277.571 | 1.249 |
Instead of merging down
here, we could have just | 4,278.82 | 2.24 |
merged back into our original chunk. | 4,281.06 | 1.68 |
And so we could have just bounced
between two separate arrays, | 4,282.74 | 2.77 |
but this is just a little more
clear in terms of remnants. | 4,285.51 | 3.51 |
So how many steps did this take? | 4,289.02 | 2.04 |
Well, how many times did I
divide my original list in half, | 4,291.06 | 4.39 |
in half, in half effectively? | 4,295.45 | 1.325 |
I had 8 elements here. | 4,299.45 | 1.58 |
And that was really like, if
you divide it up differently, | 4,301.03 | 2.74 |
two bigger halves and two bigger halves. | 4,303.77 | 1.78 |
But if you divide it again,
that's like two really big halves, | 4,305.55 | 3.17 |
and then you get one big list. | 4,308.72 | 2.03 |
So it would seem that starting
here, I divided it 1, 2, 3 times. | 4,310.75 | 4.69 |
I did some splitting. | 4,315.44 | 0.916 |
Left half, right half,
left half, right half. | 4,316.356 | 1.874 |
And any time you split something
in half, in half, in half, | 4,318.23 | 2.26 |
in half, what's the running time of
anything involving like halving again | 4,320.49 | 3.16 |
and again and again? | 4,323.65 | 0.833 |
It's like binary search. | 4,327.687 | 2.743 |
Log n. | 4,330.43 | 0.66 |
Log n. | 4,331.09 | 0.78 |
So any time you see in
computer science more | 4,331.87 | 2.76 |
generally, certainly CS50 in
upcoming weeks, this division | 4,334.63 | 2.894 |
and conquering again and
again and again where | 4,337.524 | 1.916 |
you're dividing something in half,
odds are logarithmic running time | 4,339.44 | 3.4 |
is somehow at play. | 4,342.84 | 1.34 |
But this algorithm, merge sort, surely
cannot run in big O of log n time. | 4,344.18 | 5.93 |
Because again, you can't sort n
elements in less than linear time | 4,350.11 | 5.14 |
because you'd be guessing
that everything is sorted. | 4,355.25 | 3.08 |
So log n is not the final
answer here, but there | 4,358.33 | 2.54 |
is something logarithmic happening. | 4,360.87 | 2.19 |
However, even though we did
a left half/right half thing, | 4,363.06 | 4.17 |
a left half/right half thing,
a left half/right hand, | 4,367.23 | 2.56 |
thing sort of a total of three
times, from here to here to here | 4,369.79 | 2.98 |
to here, at which point we were done. | 4,372.77 | 2.47 |
On every row that's on
the screen if you will, | 4,375.24 | 3.92 |
what did I do with my
left and right hand? | 4,379.16 | 2.66 |
I sort of always did n steps. | 4,381.82 | 5.01 |
You can really see it up here. | 4,386.83 | 1.39 |
How did I merge this last thing? | 4,388.22 | 1.594 |
I had to touch all 8 elements
by walking through the list. | 4,389.814 | 2.416 |
How did I merge these two left halves? | 4,392.23 | 2.21 |
Well, I had to do this. | 4,394.44 | 1.3 |
And then I had to do this. | 4,395.74 | 1.76 |
So if you kind of consider the remnants
of my finger touching the screen, | 4,397.5 | 4.24 |
every time I did a divide and conquer,
I had to touch every element in order | 4,401.74 | 3.6 |
to merge them together. | 4,405.34 | 1.48 |
So I did log n things. | 4,406.82 | 2.36 |
And every time I did that, I
incurred n steps of merging. | 4,409.18 | 5.13 |
So log n things times n is going
to give me big O of n log n. | 4,414.31 | 8.05 |
So this was among the options on our
sort of menu of possible running times, | 4,422.36 | 3.49 |
at least that we'll focus on right now. | 4,425.85 | 1.9 |
This is bigger than log n, of course. | 4,427.75 | 1.845 |
Because you're multiplying it by n but. | 4,429.595 | 1.625 |
It's smaller than n squared. | 4,431.22 | 1.32 |
Because if log n is less than n, then
surely n times log n is less than n. | 4,432.54 | 3.9 |
And so the running
time here of merge sort | 4,436.44 | 2.12 |
is indeed going to be big O of log n. | 4,438.56 | 5.07 |
Any questions on this here? | 4,443.63 | 3.31 |
If I may, let me point
out one other approach | 4,446.94 | 2.55 |
for seeing this same thing as follows. | 4,449.49 | 2.28 |
You can actually glean
this kind of detail | 4,451.77 | 2.5 |
more formally, especially if you're the
mathy type, from the pseudocode itself. | 4,454.27 | 3.64 |
We didn't have to walk through this
whole verbal exercise or visualization | 4,457.91 | 3.29 |
thereof. | 4,461.2 | 0.5 |
What if we just go back to basics and
just analyze our own pseudocode code, | 4,461.7 | 3.44 |
or our own C code
eventually, line by line? | 4,465.14 | 2.97 |
So here's the original algorithm. | 4,468.11 | 1.66 |
How many steps does this take to
just check if n is less than 2? | 4,469.77 | 3.58 |
Return. | 4,473.35 | 0.56 |
We're done. | 4,473.91 | 1.6 |
Big O of what? | 4,475.51 | 1.676 |
There's just one step. | 4,477.186 | 0.938 |
Or maybe, it's two, like
the if and then the return. | 4,478.124 | 2.166 |
But it's constant. | 4,480.29 | 0.77 |
It's one or two. | 4,481.06 | 0.79 |
We can debate that, but it's constant. | 4,481.85 | 1.61 |
It has nothing to do with the size of n. | 4,483.46 | 1.98 |
You're just making a finite
number of decisions there, 1. | 4,485.44 | 3.33 |
So you know what? | 4,488.77 | 1.61 |
The running time, which I'm going
to formulaically say is t of n. | 4,490.38 | 3.61 |
So the running time when
your input is of size n, | 4,493.99 | 2.86 |
is just going to be like on the
order of constant time, big O of 1, | 4,496.85 | 2.95 |
whenever n is less than 2. | 4,499.8 | 2.12 |
This is not a very bold claim. | 4,501.92 | 1.29 |
It's sort of like I'm plucking off
the easiest part of the question, | 4,503.21 | 2.48 |
but it's at least correct. | 4,505.69 | 1.22 |
If you have a small
list, it's constant time. | 4,506.91 | 2.11 |
The harder question is when we
analyze the rest of the algorithm. | 4,509.02 | 3.48 |
How many steps does it take to
sort the left half of elements? | 4,512.5 | 3.59 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.