I've written a little for loop, which is going to iterate over all of the elements in the list.
让我们先看看这儿的代码,我已经写了一个循环语句,用来迭代处理数组中所有的元素。
These are sort of the cells, if you like, in memory that are holding the elements of the list.
记忆就像是数组中的元素,我们之前说的是,我在这里开始并且比较。
Yeah, it's kind of simple, but it gives me an ordered list of these things, And let's run it. OK.
让我们来运行运行吧,好,我会先去搜索一些数组中没有的元素,让我来试试,看-1在不在这个数组里。
And if I'm walking down the list, this is probably order of the length of the list s because I'm looking at each element once.
这可能大概就是数组的长度,因为我会遍历数组中的每个元素一次,现在你可能会想,等等,数组已经排好序了。
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.
当我们在一个数组中,寻找目标元素的时候,我们不会随机的调查数组来看。
So the first thing we'll do is, we'll print the element, in this case it will be a list right? Because it's a list with two lists in it.
因此这儿我们要做的第一件事,就是要显示元素,在这儿元素就是数组了对不对?,因为这个数组包含了两个数组。
And again, this points out something I wanted to me-- I mentioned last time, list can be heterogeneous, in the sense that the elements can be multiple different types.
这儿又指出了一点我想要告诉大家的,我上节课也提到了,就是数组是多相的,也就是说元素可以是多种类型的。
But if, at any point, I get to a place in the list where the thing I'm looking for is smaller than the element in the list, I know everything else in the rest of the list has to be bigger than that, I don't have to bother looking anymore.
比当前位置数组的元素要小,我也就知道后面的数肯定,也都比我的目标数要大了,我就不用再继续进行下去了,这意味着目标数不在这个数组中,我就可以退出了。
If it's there, I'm done, if not, I keep walking down, and I only stop when I get to a place where the element I'm looking for is smaller than the value in the list., in which case I know the rest of this is too big and I can stop.
并且保持遍历,我只在当当前位置的数组元素,大于目标数时停止,这意味着剩下的元素都比目标元素大,但是其他的情况,我还是要遍历完整个数组。
I shouldn't say list, those two tuples, and walk through them to find the pieces that match up.
除数数组进行对比,我不该说数组的,是元组,遍历这两个元组找到相同的元素。
Which may take up some arbitrary amount of memory. In that case, I'm back to this problem.
然后将接下来的每一个内存块设置为,指向数组对应元素值的指针。
The easy thing to do would be the following: start at the front end of the list, check the first element. If it's the thing I'm looking for, I'm done. It's there. If not, move on to the next element. And keep doing that.
从数组的第一个元素开始:,如果这是目标元素那么结束,如果不是的话就继续比较下面的元素,并一直这么比较下去,但如果,在某一点我发现我要寻找的数字。
I've got a list, walk you through it an element at a time, do I look at each element of the list more than once?
你一次只能得到他的一个元素,我是不是把数组里面的每个元素,都过了大于等于一次?,你不这样认为么?大家有什么建议?
Could be ints, could be floats, could be a combination of things, some ints, some floats, some lists, some strings, some lists of lists, whatever.
但是我们知道数组内部的元素,可以是任何对象,可能是整数也可能是浮点数,也可能是对象的组合,一些整数。
And that's like kind of hoping that the element you're searching for is the first in the list and the last in the list. Maybe.
也就是希望要找的元素,在数组的头部或者尾部,情况可能是这样的。
The better way to think about this is, suppose, rather than starting at the beginning, I just grabbed some spot at random, like this one.
不从数组的头开始遍历,我随即的从数组中抓一个元素,比如这个,我去看看这个元素的值。
It says, well I'm going to print out first and last just so you can see it, and then I say, gee 2 if last minus first is less than 2, that is, if there's no more than two elements left in the list, then I can just check those two elements and return the answer.
然后它计算了尾点和开始点的差,如果小于2的话,也就是说数组中的元素小于等于,我对这两个元素进行比较,然后返回结果就可以了,否则的话,我们就去寻找中值点,注意它是怎么实现的,首先这个指向一个列表的开头。
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.
看看目标元素在不在数组里,也就是说我要去,检索一个有序的数组。
The worst case here is, the things not in the list in which case I've got to go all the way through the list to get to the end.
目标数不在数组里,这意味着我必须遍历数组中所有的元素,好,说完了这些。
I'm accessing a list. How long does it take for me to get the nth element of a list?
我取得数组的第N个元素,需要多长时间呢?
We can look at it here; we looked at append, which added things to lists, we looked at delete, deleting things from a list.
看看这儿,append方法给数组,增加了一些内容,我们还学习了,如何删除数组中的元素。
But we know lists can be composed of anything.
在数组中取得目标元素,是一个基本步骤。
So this is to test whether a list is a palindrome.
我们可以把1放到数组里面作为第一个元素。
I've got to count my way down, which means that the access would be linear in the length of the list to find the i'th element of the list, and that's going to increase the complexity.
的位置并去访问,然后继续下去,也就意味着,找到数组中的第i个元素的方法,是关于数组的长度呈线性复杂度的,这回增加算法的复杂度。
It's easy to think of a point as just a list of an x- and a y- coordinate.
或者说有两个元素的数组,把一个点认为是含有x坐标。
Basic idea, before I even look at the code, is pretty simple. If I've got a list that is sorted, in let's call it, just in increasing order, and I haven't said what's in the list, could be numbers, could be other things, for now, we're going to just assume they're integers.
我们可以说基本的思想是很简单的,如果我有一个排好序的数组,让我们认为这个数组是递增的吧,我并没说数组里元素是什么,可能是数字,也可能是其他的东西,现在我们假设是integer类型的数字吧,最简单的方式就是这么做了:
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个元素,是个常量时间的操作的话,我也就能像以前那样得到,这个算法是对数级复杂度的分析,并且每一步不管我选择哪个区间,我都可以把问题的规模缩小一半。
You can also assign to a list, or to an element of a list. So ivy sub 1, f -15 or example, could be assigned minus 15, and that will actually mutate the list.
此外你还可以给一个数组,或者数组内部的元素赋值,比如ivy。sub,可以被赋值为,这确实改变了数组。
Oh fudge knuckle. OK.
我找到数组中的第i个元素。
应用推荐