When you are searching a list to see whether it has an element, you don't randomly probe the list, hoping to find whether or not it's there.
当我们在一个数组中,寻找目标元素的时候,我们不会随机的调查数组来看。
If it was an unordered list, we were basically stuck with linear search. Got to walk through the whole list to see if the thing is there.
如果是一个未排序的列表,基本上我们就只能使用线性搜索了,通过遍历整个列表来查看。
Here's a list of ingredients from Skippy Peanut Butter and you see that sugar is the second most common ingredient.
这有一份四季宝花生酱的配料表,你可以看到糖是含量第二多的原料
So in the textbook, you'll sometimes see the term a strategy profile or a strategy vector, or a strategy list.
你会看到有的课本中称它为,策略组合,策略向量或者策略列表
And so now when I have the list here, let's see, yours is facing this way, so small is gonna be here large is gonna be here.
现在有了这个序列,你们是面向这边的,那小的放这边,大的放这边。
At the top of the list is Niger, with 77% of males, 93.6% of females, we see the population, in terms of Gender, 86% of the adult population is illiterate.
位于最前列的是尼日尔,77%的男性以及93,6%的女性是文盲,而忽略性别看整体人口,86%的成年人是文盲。
So I'm first going to look for something that's not in the list, I'm going to see, is minus 1 in this list, so it's going to be at the far end, and if I do that in the basic case, bam.
如果我试试第一种最基本的方法,噢,一下就完成了对不对?,因为这种方法查了下第一个元素,然后发现目标数比较下,因为目标数小于第一个元素。
Whereas the list assignment you see here did not rebind the object l 1, it changed this. OK?
并没有对包含1的对象做重新绑定,所以这有所改变,对不对?
Let's search to see though now if a million is in this list, or 10 million, whichever way I did this it must be a million, right?
不管我选哪个,数都挺大的对不对?,用嘴基本的方法,噢,花的时间有点长,好,而用二分法呢?
OK. And I'm going to look to see, is a particular element inside of that list, and again, I'll remind you, that's just giving me the integers from zero up to 9999 something or other.
一个又大又长的列表吧,好的,我打算查找列表中的一个特定元素,我会告诉你,我用的是0到大概是,9999左右的整数。
I'm going to build a list. Right, you can see that little loop going through there.
我要创建一个列表,是的,你可以看到这里有一个小小的循环。
Again, when you see that partially hydrogenated oils on an ingredient list, that means there's Trans fat.
当成分表出现"部分氢化油"时,就意味着含有反式脂肪
You find some way of systematically going through the list. Yet, I often see people, when they're debugging, proceeding at what, to me, looks almost like a random fashion of looking for the bug.
是不是目标元素,我们有一种系统化的,遍历数组的方法,然后我经常看到,有些人调试的时候。
Well let's see. My fall back is, I could just do linear search, walk down the list one at a time, just comparing those things. OK. So that's sort of my base. But what if I wanted, you know, how do I want to get to that sorted list? All right?
我只能做线性搜索了,一次遍历一遍列表,一个一个比较,但如果我想要,那怎样得到有序的列表呢?,现在的一个问题是,我们排序之前?
And the example I want to look at is, suppose I want to search a list that I know is sorted, to see if an element's in the list.
看看目标元素在不在数组里,也就是说我要去,检索一个有序的数组。
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.
首先是插入排序,在此算法中,需要遍历整个列表,将你遇到的第一个元素放在其正确的位置,然后移动到下一个元素,继续处理-,不管是什么元素,都将其插入到合适的位置。
This is a food, a common food that most people would recognize, and I'd just like to see if you could guess what it is from the list of ingredients, so take a moment to look at the list of ingredients and then I'll see if you can guess.
这是一种食品,一种大多数人都见过的普通食品,我想看看,大家能否通过配料表猜出这是什么食品吗,所以,花点时间来看看这张配料表,看看大家能不能猜出来
We're going to call binary search, it's going to take the list to search and the element, but it's also going to say, here's the first part of the list, and there's the last part of the list, Well, it checks to see, is it bigger than two?
我们将要调用这个二分查找,它将会在列表里面搜索元素,假定这里是,列表的第一个元素,那里是列表的最后一个元素,代码内部到底做了什么?
I'll let you just grok it but you can see it's basically doing what I did over there. Setting up two indices for the two sub-list,it's just walking down, finding the smallest element, putting it into a new list. When it gets to the end of one of the lists, it skips to the next part, and only one of these two pieces will get called because only one of them is going to have things leftovers.
你们可以大体的浏览一下,但是它们基本就是我在那里所做的事情,为两个子列表设置了两个指针,指针顺着列表走下去,找到最小的元素,把它放入到一个新的列表中去,当它走到一个列表的尾部时,它会跳到下部分去,两部分中只有一个会被执行,因为只有一个会有元素剩余。
With this, if I can assume that accessing the i'th element of a list is constant, then you can't see that the rest of that analysis looks just like the log analysis I did before, and each step, no matter which branch I'm taking, I'm cutting the problem down in half.
读取数组中的第i个元素,是个常量时间的操作的话,我也就能像以前那样得到,这个算法是对数级复杂度的分析,并且每一步不管我选择哪个区间,我都可以把问题的规模缩小一半。
We can put a list in the list again, as we've seen before. So let's put the list containing the string a, and I'll print out so now we see something pretty interesting about a list, that we can mix up all sorts of things in it, and that's OK.
数组放到数组中去,让我们把这个包含,字符串a的数组放进去,然后再显示,我们看到了一些,挺有趣的事情:我们可以把各种,各类的东西混合着放进去,没问题。
应用推荐