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.
这可能大概就是数组的长度,因为我会遍历数组中的每个元素一次,现在你可能会想,等等,数组已经排好序了。
And I have this, to write it out, this is order the length of the list squared, OK?
我得写下来,这是把列表的长度平方,对么?
Hmm. Well, wait a minute. If I want to sort a list, I'll take one element, I've got to look at probably a lot of the other elements in the list in order to decide where it goes. And that suggests it's going to depend on how long the list is.
我会取一个元素,然后去查看列表,来决定要把那个元素放到哪里,这就意味着它依赖于列表的长度,对,这没什么说服力,但是事实上。
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类型的数字吧,最简单的方式就是这么做了:
I could still do the linear case, which is order n or I could say, look, take the list, let's sort it and then search it. But in that case we said well to sort it was going to take n log n time, assuming I can do that.
我仍然可以做O的线性搜索,或者也可以以这个列表为例,我们先将其进行排序,然后再进行查找,但是在这种情况下,要花费n,log,n的时间去对其进行排序。
应用推荐