And so the fact that in this whole slide here, this algorithm for sorting, I'm using the verb sort.
在这个排序算法中,我用到了一个动词排序。
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.
这是此算法中很明显的一句话,也是正确解决,整个问题的关键,否则此算法就会陷入死循环。
Let's pull together what this algorithm actually does. If I generalize binary search, here's what I'm going to stake that this thing does.
总结下二分查找法,下面列举几点它的操作,首先,找中点。
It implies that this algorithm is calling itself again and again, and again, and on each time the size of the problem I'm trying to sort is being divided by what?
这就说明此算法会一次又一次地调用自己,每次我要排序的问题规模大小,会除以多少呢?
I ask you for the running time of this algorithm and you give me the running time in terms of the running time, right.
我需要得到此算法的时间复杂度,那就明确地给出其,运行时间。
So I propose this algorithm, this program, this process for counting people in this room.
因此我提出了这种算法,这种程序和处理方法,来计算教室里的人数。
Alright, I have the problem so clearly this algorithm whatever it's gonna be is gonna be at least twice as fast because I'm doing half as much work.
现在,这个问题已变得清晰,不管怎么样,排序算法的效率,至少是之前的两倍,因为我只需要做一半的工作。
Well how many more steps does this algorithm take?
用同样的算法需要多几个步骤?
What's the complexity of this algorithm?
这个算法的复杂度又是多少呢?
Now, it's also the case that this is fundamentally what class this algorithm falls into, it is going to take exponential amount of time.
哪个种类的一个实例,这个问题的时间复杂度是指数级的,也就是当n上升的时候。
And in this case, we go from 8 to 4 to 2 to 1 three times and then on each iteration of this algorithm, each pass across the board I'm touching N numbers, so that means I'm doing N things, log N times.
在这个例子中,我们从8得到4,到2,再到1,是3次,在这个算法的每次迭代中,每一趟我都会操作N个数,也就是所我每次要做N步操作,一共要做,log,N,次。
So I'm going to solve this by enumerate and check, which is an example of what's called a brute-force algorithm.
你懂的,所以我得靠列举和检验来解决这个问题,这就是我们说的穷举算法。
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.
我告诉了你们这是一个对,数级的算法,这是很棒的,我们来一起看看这个算法到底做了什么。
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?
我开始这个话题的第一个问题是,假设我问你,由这个算法实现的程序大概,要运行多久才能给出答案?
But if it watches you over some amount of time just like it's been for years with advertisements, can this kind of algorithm, this kind of process presumably improve?
但是如果它像关注广告一样,花费一些时间关注你,这些算法和进程,想必会提升吧?
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.
你可能还会依稀记得,高中数学里的对数,这就给了我们这些计算机科学家们,一些启示,即,这种算法更智能,更迅速。
Obviously the algorithm is likely to depend on the size of the input, so this is not a great idea.
很明显这个算法是与我的,输入的大小相关的,因此这不是个好方法。
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.
恩,这是一个很棒的小搜索,抱歉,和很棒的小排序算法,事实上,它依赖于一些我们要回顾的东西,被称作循环不变量。
The algorithm doesn't know that it's going to take this long to compute, it's just busy crunching away, trying to see if it can make it happen.
这很可能会发生,但是你知道的,算法本身并不知道,计算这个问题需要多长的时间,它就一直忙碌的算啊算。
What is this? What category of algorithm is that? Somebody? Louder?
这是什么呢?这种算法属于什么类别呢?,有人知道吗?大点声?
What is the class of algorithm that's- that probably applies to this, and how do I pull something out of that, if you like, a briefcase of possible algorithms to solve?
以及哪种算法-,可能可以应用到这类问题上,问题本身又能提取出什么东西来,以及可能解决问题的一系列方法的简介?
Where have we implemented a greedy algorithm, or have been asked to do a greedy algorithm? Well, there are not that many things you guys have been working on this semester.
现在你已经实施了一个贪婪算法,我们在哪里实施了贪婪算法,或者我们那里被要求实施贪婪算法呢?,们这学期不需要用什么功对吧?
This is really the guts of the algorithm 'cause this line here, someone pointed it out before.
这才是算法的核心,因为在这儿,之前已经有人指出过。
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.
我们从前面的动画中可以看到,这个归并排序算法在计算机上实现之后,绝对比其他算法更快。
So he's packing and unpacking, packing and unpacking, trying all possible combinations of objects that will obey the constraint. And then choosing the winner. Well, this is like an algorithm we've seen before. It's not greedy.
因此它不断装包和清包,尝试了所有满足约束条件的物品组合,最后选择最优者,这很像我们以前看过的一个算法,这不是贪婪算法。
The algorithm on mine professed to be implementing now is this thing.
这个算法现在要,解决的就是这个问题。
Well, let's call this same algorithm, on input of four elements.
我们调用相同的算法,其输入是4个元素。
We saw this with the sock algorithm a moment ago.
我们前面看到了袜子算法。
So again, the algorithm looks like this.
算法是这样的。
应用推荐