So this is a nice little search-- sorry, a nice little sort algorithm . And in fact, it's relying on something that we're going to come back to, called the loop invariant.
恩,这是一个很棒的小搜索,抱歉,和很棒的小排序算法,事实上,它依赖于一些我们要回顾的东西,被称作循环不变量。
OK. Binary search is perhaps the simplest of the divide and conquer algorithms, and what does that mean? It says, in order to solve a problem, cut it down to a smaller problem and try and solve that one.
好了,二分搜索也许是,最简单的分治算法,这意味着什么?,也就是说,为了解决一个问题,把它切割成小的问题,然后再来一个个解决。
So when I do the analysis, I want to think about what am I doing here, am I capturing all the pieces of it? Here, the two variables that matter are what's the length of the list, and how many times I'm going to search it?
这里,要关注的,两个变量是列表,的长度以及我要搜索的次数,这种情况下,这个算法赢了?
We've already spent a fair amount of time this semester talking about searches.
我们这个学期已经花了,很多时间来讲搜索和搜索算法了。
If we can sort things, you know, we get this n log n behavior, and we got a n log n behavior overall. But can we actually do better in terms of searching.
如果我们可以排序,如你所知,我们有n,log,n级别的算法,并且我有一个整体的n,log,n级别的算法,但是我们在搜索方面可以做的更好吗?
But let's look a little more carefully at the code for binary search-- for search 1.
二分搜索算法的代码,那么search,1,这个方法的。
应用推荐