So I'm going to solve this by enumerate and check, which is an example of what's called a brute-force algorithm.
你懂的,所以我得靠列举和检验来解决这个问题,这就是我们说的穷举算法。
Cut the problem in half. Cut the problem in half again. And that's a typical characterization of a log algorithm.
是每次除以特定的量,将问题减一半,再减一半,如此,这就是对数算法的典型特性。
So don't get too caught up in understanding the methods or statistics in a particular study or anything like that.
所以没必要过于纠结,非要去理解,某项研究的分析和算法
Now, as obvious a statement as that is in this algorithm, it turns out that is the key to this whole problem being solved correctly without my algorithm looping infinitely.
这是此算法中很明显的一句话,也是正确解决,整个问题的关键,否则此算法就会陷入死循环。
It couldn't have been a very complicated algorithm because it basically paired people up randomly on the campus.
这个算法不是很复杂,基本上是给校园里的人随机配对。
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.
我告诉了你们这是一个对,数级的算法,这是很棒的,我们来一起看看这个算法到底做了什么。
A log algorithm typically is one where you cut the size of the problem down by some multiplicative factor.
对数级复杂度的算法就是指,通过一系列常量级步数的操作,可以将问题的规模。
And you can look at it, and let's in fact take a look at it to see what it does.
你们看一看那个算法,看看到底它做了什么。
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 then the things I have to ask is how much of a division do I want to do?
如果可以这么做,那么分治算法是个好的选择,接下的问题就是我需要把问题分成多少份?
Obviously the algorithm is likely to depend on the size of the input, so this is not a great idea.
很明显这个算法是与我的,输入的大小相关的,因此这不是个好方法。
And that's because that is a version of a divide and conquer algorithm.
这个想法非常重要,因为这就是分而治之算法的一个版本。
It's a problem, as you'll see, designed to give you some practice at dealing with some of the, dare I say, more theoretical concepts we've covered in class.
这种问题会让你在处理我们,课堂上并没有讲到的更多,理论概念这方面上有更多实践,这种问题就像算法复杂度一样。
So in fact, over the next thirty or forty minutes we're going to show you a set of examples of sort of canonical algorithms, and the different classes of complexity.
在接下来的三四十分钟里面,我们将要讲一系列的,权威算法,以及不同种类的复杂度问题。
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?
合并起来,而那些小列表,我又会把他们拆成更小的列表,再排序,这会给我,一个更高效的算法么?
So this is a nice little search-- sorry, a nice little sort algorithm . And in fact, it's relying on something that we're going to come back to, called the loop invariant.
恩,这是一个很棒的小搜索,抱歉,和很棒的小排序算法,事实上,它依赖于一些我们要回顾的东西,被称作循环不变量。
And I'll remind you, we saw a set of algorithms and part of my goal was to get you to begin to recognize characteristics of algorithms that map into a particular class.
我要提醒你们的是,我们看了很多算法,我的目标之一是让你们开始,意识到算法们的特性,并且能够一一映射到特定的分类中。
Just contrast this for a brief moment to something like Selection Sort which from the get go had a ridiculous amount of redundancy comparing the same damn numbers again and again, and again.
这就与其他的排序算法形成了鲜明的对比,比如选择排序,它会一次又一次地做,多余的比较。
OK. Binary search is perhaps the simplest of the divide and conquer algorithms, and what does that mean? It says, in order to solve a problem, cut it down to a smaller problem and try and solve that one.
好了,二分搜索也许是,最简单的分治算法,这意味着什么?,也就是说,为了解决一个问题,把它切割成小的问题,然后再来一个个解决。
Now you might say, wait a minute. Thing's ordered, if I stop part way through and I throw away half the list, doesn't that help me? And the answer is yes, but it doesn't change the complexity.
如果我在半路上停下来,然后不去遍历剩下的数组了,这会有帮助么?答案是有帮助,但这没法改变算法的复杂度,因为我们之前怎么说来着?
And then we're going to turn to the question of efficiency. How do we measure efficiency of algorithms? Which is going to be a really important thing that we want to deal with, and we'll start it today, it's undoubtedly going to take us a couple more lectures to finish it off.
然后我们会回头继续讨论效率问题,我们如何计算算法的效率?,我们处理这些问题的时候,最重要的事情是什么?,我们今天要去讨论这个问题,毫无疑问我们要用,挺多节课来学习这个。
All right, having said that, let's do some examples. I'm going to show you a sequence of algorithms, they're mostly simple algorithms, that's OK.
好,让我们开始看例子吧,我将会给大家讲,一系列的算法,这些都是挺简单的算法。
Alright, so we can implement any number of algorithms using this thing because the basic mechanism I have here is a comparator.
好了,利用它我们可以实现,很多种算法,因为在此我用到的基本原理是一个比较器。
If I'm running a quadratic algorithm, it'll take one millisecond to complete.
算法会在1毫秒内完成,如果问题的复。
and we like log algorithms, because they're really fast. A typical characteristic of a log algorithm is a pro-- or sorry, an algorithm where it reduces the size of the problem by a constant factor.
并且我们也很喜欢对数算法,因为它很快,对数算法的典型特性是高速,哦,抱歉,是他能以常数因子的速度,降低问题的大小,很明显。
What you're much better at doing is saying how do I take the problem I've got and map it into a class of algorithms about which I know and use the efficiencies of those to try and figure out how to make it work.
当你碰到问题的时候,更好的方法是把遇到的问题映射,到已经设计好的算法中去,并用这些算法来提高效率,来保证问题得到解决。
And so ease of implementation is actually a very compelling -- metric against which to measure-- -- do you mind toning my voice down a bit-- is a very reasonable measure against which to measure the quality of an algorithm, right.
如此简单的实现实际上是一个很牵强的,度量,与之相对的衡量是-,如果你们不介意,我降低点音量-,是一种用来度量算法质量的,合理的方法。
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.
他们是相当棒的排序算法,是有更好的算法,我们下一次,就会看一个更好的,但是开始想想,如何完成算法,或者说是如何排序,是一个好的学习方法,恩,再试试吧,如何来排序呢?
It's a good sign that this is logarithmic, and I'm going to come back in a second to why logs are a great thing.
为什么对数级复杂度是个好事情,让我们再来看一个算法,噢,抱歉是让我们再来看两个算法。
Well, we saw the teaser in terms of that animation that suggests this merge sort algorithm when implemented by a computer is absolutely faster.
我们从前面的动画中可以看到,这个归并排序算法在计算机上实现之后,绝对比其他算法更快。
应用推荐