And so the fact that in this whole slide here, this algorithm for sorting, I'm using the verb sort.
在这个排序算法中,我用到了一个动词排序。
But I know today in my math class, we were learning about the guys who created RSA Public Key Cryptography.
但是今天的数学课上,我们在学习RSA公钥加密算法的创始人。
It's 33, 32, it's only 32 times and you can check that math at home but here lies the power of algorithmic thinking, of computational thinking as they say.
你需要33,32,对,32次,你也可以在家里自己计算,但数学算法的力量是无穷的,这就是科学家们所谓的计算机思维。
Usually what I want to talk about is what's the smallest size class in which this function grows? With all of that, what that says b is that this we would write is order b.
通常我们想要的是,方法增长时最贴切的描述,也就是说,在这个问题上,算法关于。
At the University of Minnesota, a computer algorithm paired people up.
在密尼苏达州立大学,通过计算机算法将同学分组。
If I'm running a quadratic algorithm, it'll take one millisecond to complete.
算法会在1毫秒内完成,如果问题的复。
Alright, so we can implement any number of algorithms using this thing because the basic mechanism I have here is a comparator.
好了,利用它我们可以实现,很多种算法,因为在此我用到的基本原理是一个比较器。
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.
现在你已经实施了一个贪婪算法,我们在哪里实施了贪婪算法,或者我们那里被要求实施贪婪算法呢?,们这学期不需要用什么功对吧?
And so one of the things we'll look at today is how can we leverage an algorithm, how can we implement an algorithm that at least at first glance the second time we've now seen it feels so obviously better.
因此,今天我们要学习的一点就是,我们怎样才能影响一个算法,怎样实现一个算法,使你在第二次看它时会觉得,至少比你第一次看到它时更好。
This is really the guts of the algorithm 'cause this line here, someone pointed it out before.
这才是算法的核心,因为在这儿,之前已经有人指出过。
Or another way of saying it is, we're going to use as the basic steps, those operations that run in constant time, so arithmetic operations.
我们用可以在恒定时间内完成的操作,算法,比较,内存读取。
Obviously-- and that's a bad way of saying it, I said constant the previous time-- in the linear case, it's subtract by certain amount.
这么描述很不准确,我之前在,线性算法中也说了常数,它是每次减少固定的量。
Right. Those things are going to stack up, and eventually it's going to run out of space.
不用占用其他空间的算法,对,这些东西在运行的过程中会累积起来。
N But it's definitely not one and in fact it wasn't N in the case of Selection Sort because remember the algorithm we implemented on stage last week had me going back and forth across the stage selecting on iteration, the smallest person I can find, the smallest number and then putting them into place.
但在选择排序中,肯定不会是1,也不是,注意,上周我们在这儿,实现的算法中,反复地,迭代进行选择,选出最小的数,然后将其放在合适的位置。
So the running time of the problem where the input is T of size N as expressed here formulaically, T of N, the running time of an algorithm, given an input of size N. You know what?
因此一个输入为N的问题的运行时间,在这儿的公式表示为,如果输入为N,那么此算法的运行时间,是多少呢?
So I propose this as a new algorithm for sorting N elements and being 8 in this case or really a thousand in the case of the phonebook, or anything of larger size.
所以我提出一种新的算法,来解决N个元素的排序问题,在这个问题中N是8,在电话簿的问题中N是一千,或者是大规模的任何问题。
But let's look for a slight variant of it, where greedy is not so good. And that's what's called the zero-one knapsack problem.
但是让我们找一找它的一些变种,在这些变种中贪婪算法用处不大,这些问题也就是0/1背包问题。
If I'm running a linear algorithm, it'll take one microsecond to complete.
算法会在1微秒内完成,如果是一个平方级的方法。
I make the problem ten times bigger, it takes one more step to do it.
而在线性复杂度的算法里,我把规模扩大十倍。
And we can just extrapolate in a straight line We before saw some examples where I had an algorithm to generate points, and we fit a curve to it, used the curve to predict future points and discovered it was nowhere close.
我们可以干脆用一条直线来描述它,我们之前看到在一些例子中,我用一个算法去生成一些点,然后用一条曲线对它进行拟合,然后用这条曲线来预测未来的点,最后却发现结果完全不对。
Dynamic programming is not the only time we do that.
我们在其他算法中也会用到这一点。
Here we're doing it as part of one algorithm.
在这里我们是把它当作算法的一部分来讲。
So we're to assume we can get to any piece of data, any instruction in constant time, and the second assumption we're going to make is that the basic primitive steps take constant time, same amount of time to compute. Again, not completely true, but it's a good model, so arithmetic operations, comparisons, things of that sort, we're all going to assume are basically in that in that particular model.
因此如果我们假设在恒定的时间内,我们可以取得任何一块数据,任何一种数据结构的话,我们要做的第二个假设就是,基本的原始操作计算花费的时间是恒定的,这个假设也不是完全正确的,但这个模型其实挺不错的,因此算法操作,比较,这一类的事情,我们在这个特定的模型中都假设是基本的,操作,花费的时间是恒定相同的。
And so you care-- you care about ultimately how much time is my algorithm gonna take to perform on that worst case running time.
因此你最终所关心的是,算法在最坏情况下需要多长的,运行时间。
swap I'm going to keep track of a little variable called swap, it's initially true, and as long as it's true, I'm going to keep going, but inside of the loop I'm going to set it to false, and only if I do a swap will I set it to true.
我打算跟踪一个小的变量称为,初始值为真,只要它是真的,算法就会继续,但是在循环里,我打算把他设为假,除非交换发生,就把它设为真。
Well, we saw the teaser in terms of that animation that suggests this merge sort algorithm when implemented by a computer is absolutely faster.
我们从前面的动画中可以看到,这个归并排序算法在计算机上实现之后,绝对比其他算法更快。
And we saw a couple of examples of linear algorithms.
这是线性算法的典型例子,我们在过去也看过了大量的线性算法的例子。
In the log case, it's divide by an amount.
而在对数算法中。
That's an important observation, and as we get to the part of the course we talk about computational complexity, you'll see that what we really care about is not how efficient the program is on easy problems but how efficient it is on hard problems.
我们已经学习了这门课程的一部分了,我们要开始认识到,计算的复杂度这个概念,你会学习到我们在乎的并不是,算法在简单问题上的工作效率,而是解决复杂问题的效率。
So the first one here is something called insertion sort which amounts to going through the list, taking the first thing that you see and inserting that element into its correct place, - then moving on to the next one, dealing with what element-- whatever element you're given and putting it in its right place.
首先是插入排序,在此算法中,需要遍历整个列表,将你遇到的第一个元素放在其正确的位置,然后移动到下一个元素,继续处理-,不管是什么元素,都将其插入到合适的位置。
应用推荐