text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
How many steps does it take to
sort the right half of elements? | 4,516.09 | 2.95 |
And how many steps does
it take to sort-- rather, | 4,519.04 | 5.12 |
to merge the sorted halves? | 4,524.16 | 1.56 |
So we have three questions to answer. | 4,525.72 | 2.96 |
And let me propose this. | 4,528.68 | 1.385 |
You can kind of punt. | 4,530.065 | 0.875 |
And this is the beauty of recursion. | 4,530.94 | 1.18 |
You can sort of answer the
question with the question. | 4,532.12 | 2.249 |
The running time to sort n
elements is really the running time | 4,534.369 | 3.531 |
to sort n over 2 elements, the left
half, plus the running time to sort n | 4,537.9 | 4.37 |
over 2 elements, the right
half, plus the merging step, | 4,542.27 | 3.12 |
which I claimed per my sort
of left/right finger intuition | 4,545.39 | 4.7 |
is big O of n steps linear. | 4,550.09 | 2.08 |
And now this holds if n is
greater than or equal to 2. | 4,552.17 | 3.68 |
Now, unfortunately, this is like a
recurrence if you will, mathematically. | 4,555.85 | 3.96 |
This really is a cyclical definition. | 4,559.81 | 2 |
We really need to be able to
generalize this and explain | 4,561.81 | 3.53 |
it a little more formulaically. | 4,565.34 | 2.4 |
But indeed, take a guess. | 4,567.74 | 1.45 |
If you have your old physics
textbook or your math textbook, | 4,569.19 | 2.73 |
what does this recurrence actually
equal if you multiply it all out | 4,571.92 | 3.09 |
and prove it mathematically? | 4,575.01 | 1.33 |
What's the running
time total of something | 4,578.74 | 1.75 |
where you do some function of n over
2 plus some function of n over 2 | 4,580.49 | 4.2 |
plus some number of linear steps where
each of those inner running times | 4,584.69 | 5 |
is recursively defined? | 4,589.69 | 2.03 |
And just to be clear, the
reason this is like non-obvious | 4,591.72 | 3.61 |
and should be kind of over
most heads, at least if you're | 4,595.33 | 2.75 |
less familiar with this world, is that
I'm saying that the running time, t, is | 4,598.08 | 4.15 |
a function of the running time, t. | 4,602.23 | 1.5 |
Like again, that's defining
a word with the word itself. | 4,603.73 | 4.63 |
But it turns out there are ways to
prove this inductively, mathematically. | 4,608.36 | 3.24 |
And indeed, if you actually multiply
this all out in your textbook, | 4,611.6 | 2.791 |
you will get something on
the order of n times log n. | 4,614.391 | 4.449 |
So where has that left us? | 4,618.84 | 1.204 |
We've introduced a whole number of
algorithms here, the last of which, | 4,620.044 | 2.916 |
merge sort, gives us this ability
to actually solve problems | 4,622.96 | 3.66 |
much faster than any of the others. | 4,626.62 | 2.01 |
But in each case, are we able to
apply some general formulaic analysis, | 4,628.63 | 4.34 |
asymptotic notation so to speak--
big O, big Omega, and big Theta, that | 4,632.97 | 5.35 |
describes in general terms how much
time these kinds of things take. | 4,638.32 | 5.2 |
So let's now translate one of today's
key ingredients to actual C code | 4,643.52 | 4.36 |
so that we can see how we can
leverage this programmatically. | 4,647.88 | 2.97 |
So here in CS50 IDE, I've got
an example, sigma 0 and sigma 1. | 4,650.85 | 5 |
Both of which at the end of
the day do the same thing, | 4,655.85 | 2.6 |
but they do it differently. | 4,658.45 | 1.22 |
And in sigma 0 here, we
have the following code. | 4,659.67 | 2.26 |
A sigma function that takes as input
an integer m as its sole argument | 4,661.93 | 4.96 |
and it has a return value of type int. | 4,666.89 | 1.59 |
So it's going to return a number. | 4,668.48 | 1.375 |
Main takes no command line arguments. | 4,669.855 | 1.545 |
So no arg v, no arg c this time. | 4,671.4 | 1.84 |
But inside of main is code that's going
to pester the user again and again | 4,673.24 | 3.72 |
until he or she gives us a positive
integer as per this condition | 4,676.96 | 4.43 |
down here. | 4,681.39 | 0.94 |
And then, notice down here I'm
going to call a function sigma, | 4,682.33 | 3.51 |
passing in whatever
the user's input was. | 4,685.84 | 1.85 |
10, for instance. | 4,687.69 | 1.08 |
Or 11. | 4,688.77 | 0.76 |
Or some other value. | 4,689.53 | 1.15 |
Storing the answer in answer,
and then just printing it out. | 4,690.68 | 3.67 |
And what's nice about
the fact that frankly you | 4,694.35 | 2.12 |
can't see anything else on the screen
is that for all intents and purposes, | 4,696.47 | 3.21 |
sigma, the function at the
moment, is a black box. | 4,699.68 | 2.72 |
It is a function whose purpose in life
is to add all of the numbers from 1 up | 4,702.4 | 3.23 |
until the user's input and return it. | 4,705.63 | 1.96 |
And we don't know yet or need to
care how it's actually implemented. | 4,707.59 | 3.99 |
If we do take a look inside that
black box down lower in the file, | 4,711.58 | 4.76 |
you'll see this code here. | 4,716.34 | 1.62 |
Sigma takes an input, m, as an int. | 4,717.96 | 3.08 |
Returns an int. | 4,721.04 | 0.84 |
And what does it do inside? | 4,721.88 | 1.22 |
Well, I've got what I'll
call a counter-variable. | 4,723.1 | 2.51 |
Called it sum. | 4,725.61 | 0.9 |
Initialized it to 0. | 4,726.51 | 1.38 |
And then using a for loop,
iterating from 1 up to m, | 4,727.89 | 4.01 |
because I want to sum all the
numbers from 1 up through the value | 4,731.9 | 3.63 |
the human has typed in. | 4,735.53 | 1.48 |
I just want to keep adding
to the sum that value. | 4,737.01 | 2.73 |
So it's not just sum plus plus. | 4,739.74 | 1.59 |
If I did sum plus plus as we keep
seeing syntactically in examples, | 4,741.33 | 3.684 |
that would just add 1. | 4,745.014 | 0.916 |
I want to add in whatever i is. | 4,745.93 | 2.33 |
Whether it's 1, or 2, or 3,
as I'm counting all the way | 4,748.26 | 4.16 |
up through the user's input. | 4,752.42 | 1.52 |
And then I return sum. | 4,753.94 | 1.95 |
But the neat thing
about sigma is that it | 4,755.89 | 2.76 |
affords an opportunity for recursion. | 4,758.65 | 2.13 |
This isn't necessarily a better
opportunity or more correct, | 4,760.78 | 3.66 |
but we could view the
world a little differently. | 4,764.44 | 2.35 |
Sigma1.c is identical code in main,
but the implementation of sigma | 4,766.79 | 6.29 |
is fundamentally different. | 4,773.08 | 1.58 |
And here's where it's kind of
mind-blowing, at least at first glance. | 4,774.66 | 3.21 |
You can implement a
function, like sigma, | 4,777.87 | 2.9 |
in such a way that the
function calls itself. | 4,780.77 | 3.53 |
Much like the running time for merge
sort is defined in terms of itself. | 4,784.3 | 3.76 |
Much like searching
or sorting a list, it | 4,788.06 | 2.756 |
can be recursively defined as
searching or sorting a smaller list, | 4,790.816 | 4.664 |
but with the same steps. | 4,795.48 | 1.43 |
We implement this code in C as follows. | 4,796.91 | 1.97 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.