The idea behind all these algorithms is that what's ultimately important is how many comparisons you ultimately need to make.
这些算法的思想是,你最终需要做多少次的比较,这才是真正重要的。
So at this point in the story-- at this level in the story, if you will, I've touched one, two, three, four numbers total or I've made two comparisons.
那么这时,我总共已经动过了1,2,3,4个数字,或者说我做了两次比较。
And then, when I got- I'm also going to count how many comparisons I do, just so I can see how much work I do here, and then notice what it does.
并能看到算法干了些什么-,然后是如果这个索引,比数组的大小小的话,就意味着还没检索到数组的最后。
And when we make these comparisons, one thing I want to point out is that we need to keep the constant principle quantum number constant, so we're talking about a certain state, so we could talk about the n equals 2 state, or the n equals 3 state.
当我们做这些比较时,我想指出的一件事是,我们需要保持常量原则,保持量子数是常数,所以我们在讨论一个确定的态时,我们可以谈论n等于2的态,或者n等于3的态。
And in fact, if we count up all of these silly comparisons I was making verbally I bet I'm gonna be making fewer in the end than I was with bubble or with selection.
事实上,如果将所有的,比较次数加起来,我敢打赌,比起冒泡和选择排序,它最终的比较次数是相对较少的。
Now, I put a version up there, I have no idea if this is the right way to do comparisons or not.
现在,我把一个版本放在这里了,我不知道这是不是,做比较的正确方法。
So this is doing comparisons.
那么这个操作是在做比较。
So in the end I am doing comparisons.
最终我还是得进行比较。
So how many comparisons did that take here?
那这一过程用了几次比较呢?
So that's four comparisons.
比较了4次了。
Seven comparisons, right?
是我花了多少次操作来完成这件事?
Selection sort too really reduces to a total number of comparisons because I'm again comparing the current smallest to the next thing I see, the next thing, so really a lot of these sorting algorithms boil down to comparisons and the numbers that you actually have to make.
选择排序也可归纳为总数的比较,因为要将当前最小者与下一个进行比较,接着再下一个,可见,很多排序算法都可归结为比较,以及需要比较的次数。
With that mechanism we can now implement these comparisons sorts.
利用这一原理,现在我们就可以实现,这些比较排序了。
N-2 That's six comparisons, so N minus 2.
次了,正好是。
So we're to assume we can get to any piece of data, any instruction in constant time, and the second assumption we're going to make is that the basic primitive steps take constant time, same amount of time to compute. Again, not completely true, but it's a good model, so arithmetic operations, comparisons, things of that sort, we're all going to assume are basically in that in that particular model.
因此如果我们假设在恒定的时间内,我们可以取得任何一块数据,任何一种数据结构的话,我们要做的第二个假设就是,基本的原始操作计算花费的时间是恒定的,这个假设也不是完全正确的,但这个模型其实挺不错的,因此算法操作,比较,这一类的事情,我们在这个特定的模型中都假设是基本的,操作,花费的时间是恒定相同的。
STUDENT: Let me compare y to Yeah, it's not doing all of the comparisons.
学生:让我把y和【不可闻的声音】做比较,对了,它并没有对所有的情况作比较。
It took me seven comparisons, because I can take advantage of the fact I know I only ever have to look at the first element of each sub-list. Those are the only things I need to compare, and when I run out of one list, I just add the rest of the list in.
进行了7次对比,因为我可以利用我知道的优势:,每次只需要比较每个子列表的第一个元素,那才是我需要进行对比的内容,当一个列表的元素处理完了,只需要将另一个列表剩下的元素直接添加进去。
Alright, that's 5 comparisons.
次。
应用推荐