These things run for variable amounts of time because they take input, a list of size of some amount.
而这一段的执行时间是可变的,因为它们需要输入一个包含一些元素的列表。
Seventeen against thirty, twenty-four against thirty And by this stage I've got nothing the rest of that list in.
这个列表中已没有任何元素了,那么我可以将那个列表里剩下的元素,添加进去。
Ignore this one here 'cause this is when I was just writing down the original list of size 1.
暂且忽略这个,因为它是大小为1的列表中的元素,我只是把它写下来而已。
So wherever the largest element started out in the list, by the time I get through it, it's at the end.
所以无论最大的元素,在列表的什么位置。
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应该是合并后列表的,下一个元素,然后你们就知道了。
OK. So, soon as I get down to a list that has no more than two elements in it, I'm done.
不超过2个元素的列表,那就结束了,注意,这里是小于等于。
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个元素的列表,然后我以这些没有实际意义的字符将其填充。
Advance this pointer-- this finger, to the next element of the list which is 4, make the comparison.
前移这个指示器--也就是这根手指,让它指向列表中的,下一个元素4,再做比较。
And then, if I want to find out if something's there, I do the same thing. But notice now, hash is converting the input into an integer.
然后,我想查找出某些元素是否在列表中,我做同样的事,但是现在注意,哈希是将输入转化成一个整数,所以,其思想是什么?
Again. Basic premise of binary search, or at least we set it up was, imagine I have a sorted list of elements. We get, in a second, to how we're going to get them sorted, and I want to know, is a particular element in that list..
好,二分查找的基本前提,或者是我们建立二分查找的基础,我们已经有了一个排好序的元素列表,我们就需要知道如何来快速的排序,如何从列表中找到特定的元素。
If there are less than two elements there I just check one or both of those to see if I'm looking for the right thing.
列表里是否剩余超过两个元素?,是否剩余不足两个元素,这里面我就挑一两项来检查下是否运行正常。
This is sorted, this is sorted, how do I now make a list of size 2?
这个是有序的,这个也是有序的,我怎样才能组成一个有2个元素的列表?
I look at the rest of the list to decide what's the element to swap into the next place.
我会查看列表剩下的部分,来看看需要交换哪个元素。
If I do this, try and get the 10th element of a list that's only eight long. I get what looks like an error, but it's actually throwing an exception.
如果我来这么做,试着去取长度为8的列表的第十个元素,我貌似能得到一个错误。
I'm walking along the list once, taking two things and saying, make sure the biggest one is next.
我遍历一次列表,每次取两个值,确认最大的元素在后面一个。
And in the next spot, maybe I've got a really long list, and I'm going to say how to jump to get to the next element.
可能我得存放一个很长的列表了,你们也明白如何定位下一个元素,好的,这很棒,这样我有一个。
If I look for, say, minus 1, you might go, gee, wait a minute, if I was just doing linear search, I would've known right away that minus one wasn't in this list, because it's sorted and it's smaller than the first elements.
如果我要查找-1,你可能要怒了,呵呵,等一等,如果我用的是线性查找,我不会知道-1不在这个列表中,但是列表是排好序的,1又比第一个元素小。
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.
我会取一个元素,然后去查看列表,来决定要把那个元素放到哪里,这就意味着它依赖于列表的长度,对,这没什么说服力,但是事实上。
Let me divide this into 2 lists of size 1 and now done, right?
那么我将其划分为只有1个元素的2个列表?
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左右的整数。
That one's not so obvious. So let's think about this for a second. To sort a list in linear time, would say, I have to look at each element in the list at most a constant number of times.
所以让我们来思考一会,要在线性时间能排序,列表里每个元素最多被使用常数次,不一定是一次,对吧。
Sure. So that says one should be the first element in my merge list.
所以可以确定1应该是合并后的,列表的第一个元素。
Well, I might move my hands here 'cause this list only has one element left.
我会将手移到这边,因为这个列表中,只剩下一个元素了。
I can now just blindly copy what remains in the previous list here.
我现在就可以直接将,前面列表中的元素拷贝到这儿。
This one here, this one here, the lightest elements of each are where?
一个在这,另一个在这,每个列表中最轻的元素在哪呢?
What if I just tested to see if it was only, say, one?
列表里只有一个元素?
I just go down the list selecting the smallest person at a time and then I repeat, repeat, repeat but when we actually did out the math or kind of reason through it, the running time, the asymptotic running time of bub-- of Selection Sort was also what?
只需要遍历列表,每次找出最小的元素,然后重复上述步骤,但从数学角度看,选择排序的时间复杂度,又是多少呢?
What's this thing doing? It's walking along the list looking for the smallest thing in the back end of the list, keeping track of where it came from and swapping it with that spot in the list. All right?
这是在做什么?是在遍历列表,找列表后端最小的元素,跟踪它从哪里来的,然后同当前点的元素交换,明白了么?
So now I have to merge these two lists of size 2.
现在我需要合并这两个各有2个元素的列表。
And that's where I said we've got to be careful.
正在接近列表里面的那个元素。
应用推荐