So I'm going to solve this by enumerate and check, which is an example of what's called a brute-force algorithm.
你懂的,所以我得靠列举和检验来解决这个问题,这就是我们说的穷举算法。
And so the fact that in this whole slide here, this algorithm for sorting, I'm using the verb sort.
在这个排序算法中,我用到了一个动词排序。
We started off talking about binary search, and I suggested that this was a log algorithm which it is, which is really kind of nice.
我告诉了你们这是一个对,数级的算法,这是很棒的,我们来一起看看这个算法到底做了什么。
It couldn't have been a very complicated algorithm because it basically paired people up randomly on the campus.
这个算法不是很复杂,基本上是给校园里的人随机配对。
A log algorithm typically is one where you cut the size of the problem down by some multiplicative factor.
对数级复杂度的算法就是指,通过一系列常量级步数的操作,可以将问题的规模。
And the obvious question I could start with is, and suppose I ask you, how long does the algorithm implemented by this program take to run?
我开始这个话题的第一个问题是,假设我问你,由这个算法实现的程序大概,要运行多久才能给出答案?
You might remember vaguely logarithms from high school math and such but what this suggests for us , the computer scientists, is that this is certainly a smarter, a faster algorithm.
你可能还会依稀记得,高中数学里的对数,这就给了我们这些计算机科学家们,一些启示,即,这种算法更智能,更迅速。
And that's because that is a version of a divide and conquer algorithm.
这个想法非常重要,因为这就是分而治之算法的一个版本。
So what we want to do, is, I guess another way of saying it is, efficiency is really about choice of algorithm.
因此我们想做的就是,换种说法,效率实际上是算法的选择,我们想要培养你们。
Why don't I just keep track on each pass through the algorithm whether I have done any swaps?
我为什么不仅仅跟踪算法的,每一遍来看看我做了哪些交换呢?
All right? Because if I don't do any swaps on a pass through the algorithm, then it says everything's in the right order.
好么?因为如果我,这一遍没有做任何交换,那么就意味着已经排好序了。
I want you to recognize classes of algorithms and match what you see in the performance of the algorithm to the complexity of that algorithm. All right?
让我再试一次这么说,我期望大家能通过,算法的复杂度,来看到算法的性能?
And so you care-- you care about ultimately how much time is my algorithm gonna take to perform on that worst case running time.
因此你最终所关心的是,算法在最坏情况下需要多长的,运行时间。
They are computationally challenged, meaning, at the time they were invented, they were perfectly good sorting algorithms, there are better ones, we're going to see a much better one next time around, but this is a good way to just start thinking about how to do the algorithm, or how to do the sort.
他们是相当棒的排序算法,是有更好的算法,我们下一次,就会看一个更好的,但是开始想想,如何完成算法,或者说是如何排序,是一个好的学习方法,恩,再试试吧,如何来排序呢?
Well, we saw the teaser in terms of that animation that suggests this merge sort algorithm when implemented by a computer is absolutely faster.
我们从前面的动画中可以看到,这个归并排序算法在计算机上实现之后,绝对比其他算法更快。
Again, I want you to start seeing how to map the characteristics of the code- the characteristics of the algorithm, let's not call it the code-- to the complexity.
是算法中的那些特性,我们还是别说代码了,转化为复杂度-,我一会儿还会再讲点儿这个的,但是我们再看一个例子。
Well, let's call this same algorithm, on input of four elements.
我们调用相同的算法,其输入是4个元素。
Well how many more steps does this algorithm take?
用同样的算法需要多几个步骤?
Where those pieces, I would do the same thing with, I would divide them up into smaller chunks, and sort those. Is that going to give me a more efficient algorithm?
合并起来,而那些小列表,我又会把他们拆成更小的列表,再排序,这会给我,一个更高效的算法么?
It's an example of a very common tool that's going to be really useful to us, not just for doing search, but for doing a whole range of problems. That is, in essence, the template the describes a log style algorithm.
不仅仅是做搜索,还可以解决一整类问题,本质上,这个模板就描述了,对数形的算法,我们一会再回来。
All right? I don't care what's in there, but just assume that's my list. And just to remind you, on your handout, and there it is on the screen, I'm going to bring it back up, there's the little binary search algorithm.
好,我不管列表里是什么,就假定那是一个列表,要提醒你们的是,在你们的讲义上和屏幕上,有一个简单的二分查找算法。
It will probably quit at some point because of space issues, unless I'm writing an algorithm that is using no additional space.
其中一个的时候,它会一直运行下去,它可能在某一处因为空间问题退出,除非我写了一个。
What is this? What category of algorithm is that? Somebody? Louder?
这是什么呢?这种算法属于什么类别呢?,有人知道吗?大点声?
And you really, wherever possible, want to avoid that exponential algorithm, because that's really deadly. Yes.
比你的电脑的性能增长的快多了,并且大家无论何时,都要避免去用指数级的函数。
What do you get if you follow the greedy algorithm? What's the first thing the thief does?
如果年贪婪算法你会带走什么呢?,这个贼做的第一件事是什么?
We'll start with the greedy thief. Well, the greedy thief follows the greedy algorithm.
我们从贪婪的贼开始,这个贪婪的贼按贪婪算法行动。
If I'm running a linear algorithm, it'll take one microsecond to complete.
算法会在1微秒内完成,如果是一个平方级的方法。
Typical characterization, not all the time, but typical characterization, is an algorithm that reduces the size of a problem by one, or by some constant amount each time, is typically an example of a linear algorithm.
我们学习过了线性算法,它的典型特征,不是通用的,但是比较典型的特征是,它是逐一减小问题的大小的,或者说是每次减小常数的大小。
We saw this with the sock algorithm a moment ago.
我们前面看到了袜子算法。
Here we're doing it as part of one algorithm.
在这里我们是把它当作算法的一部分来讲。
应用推荐