text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
could use or equations to express the running time of algorithms.
2,750.48
3.89
But many of the algorithms we'll look at in CS50 certainly
2,754.37
2.47
fall into these categories.
2,756.84
1.67
So let's take an example.
2,758.51
2.89
What algorithm has the running time of big O of n squared?
2,761.4
2.73
Well, bubble sort for one thing.
2,764.13
3.02
And it turns out selection sort.
2,767.15
2.12
And it turns out insertion sort.
2,769.27
2.84
All three of those algorithms actually have the same running time,
2,772.11
3.81
even though they're fundamentally different in execution.
2,775.92
3.44
But they do share a commonality.
2,779.36
2.01
All of them are comparison-based.
2,781.37
2.72
In each case did we actually compare values,
2,784.09
2.31
either adjacent or we would look at a number there, compare it to a number
2,786.4
3.33
here, and then decide whether or not to do a switch.
2,789.73
2.234
So they were all comparison-based.
2,791.964
1.416
And all of them, frankly, involved quite a bit of movement or walking,
2,793.38
3.24
either on my part or the humans-- volunteers part.
2,796.62
2.48
For instance, in bubble sort's case, I had
2,799.1
2.64
to walk through the whole list, sorting elements pairwise like this.
2,801.74
3.425
But that wasn't enough.
2,805.165
1.575
I then had to do it again and walk across the stage.
2,806.74
4.14
And then I had to do it again and walk across the stage.
2,810.88
2.78
And this is what n squared feels like.
2,813.66
1.627
And even though the problem was getting a little smaller
2,815.287
2.333
as the big elements bubbled up, mathematically, even that algorithm,
2,817.62
4.32
it's kind of essentially like doing n things, looking at n people,
2,821.94
3.76
n times, iterating n total times, which is roughly n times n, or n squared.
2,825.7
4.517
It's just going to add up a lot.
2,830.217
1.333
So what about selection sort?
2,831.55
1.61
What did I do in selection sort's case?
2,833.16
1.65
Here, it's even more obvious, perhaps.
2,834.81
1.8
To find the smallest element in a list, I
2,836.61
2.2
have to look logically at every element before I can confidently conclude,
2,838.81
5.105
yes, I have found the smallest element.
2,843.915
1.625
Once you find it, you can plop it at the beginning.
2,845.54
2.125
And that's easy.
2,847.665
0.785
But then you have to find the second smallest element.
2,848.45
1.95
And even though we humans could kind of eyeball things and realize,
2,850.4
2.791
oh, 2 is the next smallest.
2,853.191
1.249
You don't know that as the computer unless you look at every element.
2,854.44
2.91
Ah, I found 2.
2,857.35
1.49
And then you could put 2 where it belongs.
2,858.84
1.75
So again, there's this walking back and forth.
2,860.59
2.18
And if you're going to walk as many as n steps and have to do this n times--
2,862.77
3.9
one for every element-- that, too, is n times n.
2,866.67
2.13
And insertion sort, same damn problem, even though it's
2,868.8
2.9
sort of logically different, the algorithm.
2,871.7
1.91
Insertion sort, beautifully I only move one step at a time
2,873.61
4.51
and I never turn back.
2,878.12
1.14
Because I just deal with, as you did for the blue books, each problem one
2,879.26
5.24
at a time, never again backtracking.
2,884.5
2.36
But where was all the work happening?
2,886.86
3.34
In the shifting of humans.
2,890.2
1.44
Now, technically you guys helped out by doing the shifting yourselves.
2,891.64
2.92
But if I'm the computer, it actually would
2,894.56
1.749
have been like me tiptoeing back, shifting, shifting, shifting,
2,896.309
3.391
inserting, shifting, shifting, shifting.
2,899.7
2.18
So again, in the worst case, I might have
2,901.88
1.71
had to shift everyone all the way over to make room for someone.
2,903.59
3.94
Because in the worst case, what's the worst possible input
2,907.53
2.55
you might get to a sorting algorithm?
2,910.08
3.4
What input would create the most work for you?
2,913.48
3.805
AUDIENCE: Backwards.
2,917.285
0.86
SPEAKER 1: Backwards.
2,918.145
0.875
If the list is perfectly backwards, every darn element
2,919.02
2.69
is out of place, which means you have to traipse back and forth
2,921.71
3.14
and back and forth to insert or to select or to swap all of those elements
2,924.85
5.85
that many times.
2,930.7
1.1
So in all of these cases, they are not numerically identical.
2,931.8
3.53
Some of those algorithms might take fewer steps than others in totality,
2,935.33
3.97
but they're all on the order of n squared for the reason
2,939.3
2.86
that they're all involving comparisons as many as n times n times.
2,942.16
4.79
All right.
2,946.95
0.68
What about an algorithm that's big O of n?
2,947.63
3.2
An algorithm that only takes n steps?
2,950.83
3.65
We've seen one.
2,954.48
0.878
Derek showed us one.
2,958.05
1.26
What's an algorithm that takes n steps?
2,962.45
1.68
Derek, hopefully.
2,964.13
1.094
AUDIENCE: [INAUDIBLE].
2,965.224
0.916
SPEAKER 1: A random search?
2,966.14
1.49
Kind of, sort of.
2,967.63
1.07
It's a little harder to put a number to because it
2,968.7
3.244
depends what you mean by "random."
2,971.944
1.416
Because if you have random with repetition such
2,973.36
2.11
that you're checking foolishly the same elements again and again,
2,975.47
2.877
it can actually add up to more than n steps.
2,978.347
1.833
So not random, but-- yeah.
2,980.18
2.805
AUDIENCE: Binary.
2,982.985
0.8
SPEAKER 1: Binary is even better.
2,983.785
1.375
Hold that answer.
2,985.16
0.708
You're about to be correct.
2,985.868
2.012
Linear search, right?
2,987.88
1
If you just blindly go from left to right
2,988.88
1.88
or right to left looking for some element,
2,990.76
1.81
that is on the order of n steps.
2,992.57
1.49
Because you are just doing foolishly unnecessary
2,994.06
2.61
work if you go beyond the end of the list
2,996.67
2.2
or beyond the beginning of the list.
2,998.87
1.5