I've thrown away most of the array- most of the list, I shouldn't say array- most of the list. All right?
我已经把数组的大部分都排除了-,列表的大部分-,我不应该说数组-列表的大部分?
So, now, here's just a list for you: eight features of literary modernism that are all important to Nabokov.
现在我给大家一张列表,上面列举了八项对纳博科夫非常重要的文学现代主义的特征。
If I want to create a set or a sequence representing these things, I simply insert into that list.
如果我想创建一个集合,或者一个序列表示这些东西,我只要简单的把它插入列表中。
So finger-pointing at the start of each list, 6 is indeed less than 8 so I'm gonna write it down first.
手指指向列表的开始,6肯定比,8小,我先把它写下来。
I haven't said how I'm going to get those sorted lists, but imagine I had two sorted lists like that.
我还没有说明我怎么才能得到已排序的列表,但是想像一下我现在已经有,两个已排序好序的列表了。
So wherever the largest element started out in the list, by the time I get through it, it's at the end.
所以无论最大的元素,在列表的什么位置。
Ignore this one here 'cause this is when I was just writing down the original list of size 1.
暂且忽略这个,因为它是大小为1的列表中的元素,我只是把它写下来而已。
So supposed that the list is not of size 8, let's make it slightly more interesting and double it.
假如列表的大小不是,为了更有趣,使其加倍。
So it's certainly at least linear in the length of the list. For each starting point, what do I do?
它至少是线性的计算列表的长度,每次到了循环开始的点?
Now, compare the first element in each of these lists. Two is less than three, so two ought to be the next element of the list.
现在,比较每个列表的第一个元素,2要比3小,所以2应该是合并后列表的,下一个元素,然后你们就知道了。
And I want to show you a little bit more detail of that, so let's create a simple little list here.
我还想给你们再讲详细些,那让我们来创建一个简单的列表吧。
OK. So, soon as I get down to a list that has no more than two elements in it, I'm done.
不超过2个元素的列表,那就结束了,注意,这里是小于等于。
Still quadratic, right? I'm looking for the worst case behavior, it's still quadratic, it's quadratic in the length of the list, so I'm sort of stuck with that.
还是平方,对吧,我在寻找最坏的情况,它还是平方,它是列表长度的平方,我对此有点无奈了。
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?
合并起来,而那些小列表,我又会把他们拆成更小的列表,再排序,这会给我,一个更高效的算法么?
Well, it looks like it took 1 in this case or it involves-- we can put it another way, merging those two lists involved looking at two numbers, 1, 2, and that's it.
在这种情况下看起来只用了1次-,我们可以从另一个角度看,合并这两个列表涉及到了,2个数字,1,2,就是这样。
Run merge sort on those. By induction, if it does the right thing, I'm going to get back two lists, and I'm going to then merge Them together. Notice what I'm going to do.
在这些上面再运行归并排序,根据归纳,如果这样是正确的,我将重新得到两个列表,然后我会把它们合并在一起。
Well, notice again, on each level of the tree, when I actually do the merging, so this remember, is when I wrote down just the numbers when I bottomed out with the list of size 1.
请注意,在这棵树中的每一层,我真正做合并时,就将每个大小为1的列表中的,数字写下来。
All right? And I'm going to say-- sort of set that stage here, so that-- It turns out that that's probably about the best we can do, or again ends at the length of the list.
我要说的是,这一阶段的集合,我们用最优的方法完成,还是取决于列表的长度,好的,还是回到了我的问题上了。
OK? So you can see that, it's just walking through it, and in fact if I look at a couple of another-- another couple of examples, it's been a long day, again, you can see that property.
恩,其实你能看到,他就是遍历列表,你要有空的话,可以再看很多很多其他的例子,你能够看到这个属性。
First, it's pointing to the beginning of the list, which initially might be down here at but after a while, might be part way through. And to that, I simply add a halfway point, and then I check.
列表中间的一个部分了,然后我求出列表的中值点,然后看看该点的值是不是等于目标值,如果是的话就完成了,如果不是的话,如果中位值大于我要找的目标值。
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.
好,我不管列表里是什么,就假定那是一个列表,要提醒你们的是,在你们的讲义上和屏幕上,有一个简单的二分查找算法。
So when I do the analysis, I want to think about what am I doing here, am I capturing all the pieces of it? Here, the two variables that matter are what's the length of the list, and how many times I'm going to search it?
这里,要关注的,两个变量是列表,的长度以及我要搜索的次数,这种情况下,这个算法赢了?
So now here's the idea. I build a list 256 elements long, and I fill it up with those special characters none.
所以这里就是这种思想6,我创建了一个可以放下256个元素的列表,然后我以这些没有实际意义的字符将其填充。
And what does that say? It says, let's assume I want to do k searches of a list. OK.
如果我们假定要在列表中做k次搜索,在线性的情况下,假定是一个未排序的情况。
And I have this, to write it out, this is order the length of the list squared, OK?
我得写下来,这是把列表的长度平方,对么?
Remember we said we design lists so that the access, no matter where it was on the list was of constant time.
记住我们说过我们创建了,一个列表它就是这么访问的,无论它在列表的哪个位置。
How do I figure that out? Well I'm looping down the list, right? I'm walking down the list.
怎么算出来的呢?我在循环遍,历这个列表对吧?那要遍历这个列表。
Advance this pointer-- this finger, to the next element of the list which is 4, make the comparison.
前移这个指示器--也就是这根手指,让它指向列表中的,下一个元素4,再做比较。
Just to give you an example, here's one list, 1 2 4 30 3121724 Here's another list, 12430.
举个一例子来说,一个列表:3,12,17,24,另一个列表:
And typically, I'm not going to just search once in a list, I'm going to search multiple times.
还要考虑我要如何去使用它?,我不会对一个列表只做一次搜索。
应用推荐