And the third thing I need to decide is how do I combine? You know, point out to you in the binary search case, combination was trivial. The answer to the final search was just the answer all the way up.
第三个问题是我需要决定如何进行合并?,就你们所知的,在二分查找中所打印出来的,合并的过程是非常简单的,最后查询的结果,就是一路上来所以的结果。
So at the moment left hand is at the start of this list of size 1, my right hand is at the start of this list of size 1, and now I need to merge these two lists.
现在左手手指指向这个大小为1的列表的开始,右手手指指向这个大小为1的列表的开始,现在我需要合并这两个列表。
How much work do I have to do to actually merge them together?
我需要耗费多大的工作量来他们进行合并?,让我来举个例子?
But we have one last step which is obviously merge the sorted halves.
现在还需要最后一步,很明显就是合并有序的两半。
So now I have to merge these two lists of size 2.
现在我需要合并这两个各有2个元素的列表。
So now, I need to merge the sorted halves.
现在,我需要合并已排好序的部分。
Well I need to merge these two lists.
我需要将这两个序列进行合并。
How hard is it to merge them?
将它们进行合并需要耗费多少时间?嗯?
So now, I have to merge.
需要合并。
Notice here that it's different than the binary search case. We're certainly dividing down, but the combination now actually takes some work. I'll have to actually figure out how to put them back together. And that's a general thing you want to keep in mind when you're thinking about designing a divide and conquer kind of algorithm.
一个分治的例子,注这里,与二分查找所不同的地方,我们肯定是分解了,但是合并的过程还是需要一些工作量的,我会详细说明怎样把它们合并在一起的,当你在考虑设计一个分治算法时,这是你要必须记住的最基本的东西。
So if on each iteration of merging I'm doing eight things or more generally, N. That then begs the question, how many levels of this tree are there actually?
可见对于合并我需要迭代8次,一般情况下是N,这取决于具体问题,那么在这棵树中一共有多少层呢?
But that merging process only takes N steps, N*log N so that's N times log of N. Now, it's a little tricky to reason through this perhaps the first time, let's just take a very simple example and see if we can do a little sanity check here.
但这个合并过程只需要N步,所以时间复杂度是,第一次对此进行推论可能会有点儿棘手,我们举一个简单的例子,看看我们能否做一些完整性的检查。
So I always have to do a merge of n elements.
所以我始终需要做n个元素的合并。
应用推荐