So the integers are totally ordered the reals are totally ordered lots of things are, the rationals are totally ordered.
对正数来说,这样的序列是排好序的,对实数也是如此,对很多事情都是如此。
I have sorted with the smaller problem 1 because that smaller problem right now is of size 1 and so it's sort of obviously the case that this cup is now sorted.
对这个较小的问题我已经排好序了,因为在这个小问题中只有1个元素1,那么很明显,这个杯子已经是有序的了。
And that you might look at, for example, that first example, and say, man by this stage it was already sorted, yet it had to go through and check that the third element was in the right place, and then the fourth and then the fifth and then the six.
你可能会去看看例如第一个例子,然后要抱怨,到这里,它已经是排好序的了,但他还是得去遍历查看,第三个元素是不是在正确的位置,然后第四个,第五个,第六个。
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..
好,二分查找的基本前提,或者是我们建立二分查找的基础,我们已经有了一个排好序的元素列表,我们就需要知道如何来快速的排序,如何从列表中找到特定的元素。
Let's assume that I could somehow get to the stage where I've got two sorted lists.
让我们来假设目前的情况是:,我已经有两个已排好序的列表。
- So again, if you're unwinding what's going on here, this-- we sorted the left half which meant sort the left half, then the right half then the merge.
同样,如果你展开正在进行的一切-,我们已对左半部分排好了序,接着右半部分,接着合并。
I remind you, I know you're not really listening to me, but that's OK. I reminded you at the beginning of the lecture, I said, let's assume we have a sorted list, and then let's go search it.
没关系,我告诉过你在课程的开始,我们假设这是一个排好序的列表,然后才进行的搜索,那实际上有序列表从哪里来的呢?
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又比第一个元素小。
We don't seem to be doing that just yet, certainly not as badly, alright, so at this point in the story I have a sorted list of size 4.
当然现在我们不需要那样做,此时此刻,我已对整个问题中大小为4的列表排好序了。
At the end of the first round, I've got the smallest element at the front. At the end of the second round, I've got the smallest two elements at the front, in fact I got all of them sorted out. And it actually runs through the loop multiple times, making sure that it's in the right form.
看看发生了什么,在第一轮结束后,我把最小的元素移到了前面,第二轮结束后,我把最小的,两个元素移到了前面,实际上,所有的元素都排好序了,实际上,这个算法运行了几次循环,确认下这是正确的形式。
So now, I have a list that's sorted of size 2.
现在大小为2的列表已排好序了。
And this is now consistent with my claim that I have sorted a list of size N equals 1.
这与我之前所说的是一致的,我已经将N为1的一个序列排好了序。
So now, I need to merge the sorted halves.
现在,我需要合并已排好序的部分。
The merge, the sorted halves.
合并排好序的两部分。
So let's look at that.
假设我想搜索一个已经排好序的数组。
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类型的数字吧,最简单的方式就是这么做了:
应用推荐