求一个二进制搜索算法_二进制搜索

本文介绍了二进制搜索算法的高效性和工作原理,通过举例说明如何在一个有序序列中快速定位目标值。二进制搜索每次比较都能将搜索空间减半,确保在O(lgN)的时间复杂度内找到解决方案,相较于线性搜索,其优势在于查找速度极快。
摘要由CSDN通过智能技术生成

求一个二进制搜索算法

Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on page 104000. How do we do that? Go to every page one by one? No, linear search would take multiple days to do that. For simplicity, let’s assume there are 18 pages in the book and we want to find out the content of page 16. Then we will go back to how binary search is very fast and efficient for a gigantic search space.

线性搜索(逐个搜索数组中的每个索引)几乎可以在任何地方使用,但在许多情况下并不是最佳选择。 假设,我们有一本书,共有42949672960页。 我们也有一个目录。 现在我们要阅读第104000页的内容。我们该如何做? 一页一页地转到每一页? 不,线性搜索需要花费几天的时间。 为了简单起见,我们假设书中有18页,并且我们想找出第16页的内容。然后,我们将回到二进制搜索对于巨大的搜索空间如何非常快速和高效的方面。

  page numbers ->   1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 

页码-> 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |

At first, we divide the book into two equal parts and find middle value which is (1 + 18) / 2 or 9 (we are taking floor value of 9.5). First part has page 1 to 9, second part has page 10 to 18. We can definitely tell that first part does not have our target page because the highest page which is 9 is less than 16. So, we eliminate that part.

首先,我们将这本书分为两个相等的部分,并找到中间值(1 + 18)/ 2或9(我们将底值设为9.5)。 第一部分的页面为1到9,第二部分的页面为10到18。我们可以肯定地说第一部分没有目标页面,因为最高的页面9小于16。因此,我们将其删除。

page numbers ->   1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 

页码-> 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |

Now, we have page 10 to 18 to search. Let’s divide it again, middle value is (10 + 18) / 2 or 14. First part has page 10 to14, second part has page 15 to 18. Again, eliminate the first part as 16 is in the range of 15 to 18. We repeat this procedure until we get a middle value which is equal to our target value 16.

现在,我们有10至18页可供搜索。 让我们再次将其除以,中间值为(10 + 18)/ 2或14。第一部分的页面为10到14,第二部分的页面为15到18。再次,将第一部分删除,因为16在15到18的范围内。我们重复此过程,直到获得等于目标值16的中间值。

page numbers ->  10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |  

页码-> 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |

Current search space is 15 to 18, mid = (15 + 18) / 2 or 16, which is our target value. So, we stop here. One thing to keep in mind that binary search works only for sorted sequence items where we eliminate our search space based on their median and target value. Here is a coding example for binear search implemented in Java.   

当前搜索空间为15到18,中=(15 + 18)/ 2或16,这是我们的目标值。 因此,我们在这里停止。 要记住的一件事是,二进制搜索仅适用于排序的序列项,在这些序列项中,我们根据它们的中值和目标值消除了搜索空间。 这是用Java实现的二进制搜索的编码示例。

// getKeyIteratively() takes 2 parameters
// 1. array of pages 2. target value
// Returns the index of target value/key
// If target value is not found, it returns -1
public int getKeyIteratively(int[] pages, int key) 
{
	int length = pages.length;
	int lo = 0, hi = length - 1;
	while(lo <= hi) 
	{
		int mid = (lo + hi)/2;
		// if value is target value
		if(pages[mid] == key) return mid;
		else if(key < pages[mid]) hi = mid - 1;
		else lo = mid + 1;
	}
	// key not found
	return -1; 
}

Complexity

复杂

Each comparison divides the array into two parts and considers only one half. This makes our search space half of the previous search space. That is why, it is gaurenteed to find a solution in O(lgN) comparison (if there is a solution) in an array of N items. Going back to  2^32 or 42949672960 pages we would have to perform at most 2^32 comparisons to find our result using linear search. In case of binary search we would need only lg(2^32) or 32 comparisons which is a very small number compared to 2^32.

每个比较将数组分为两部分,只考虑一半。 这使我们的搜索空间是先前搜索空间的一半。 这就是为什么要在N项数组中的O(lgN)比较(如果有解决方案)中找到解决方案的原因。 回到2 ^ 32或42949672960页,我们最多只能执行2 ^ 32比较,才能使用线性搜索找到结果。 在二进制搜索的情况下,我们只需要lg(2 ^ 32)或32个比较,与2 ^ 32相比这是一个非常小的数字。

翻译自: https://www.experts-exchange.com/articles/17324/Binary-Search.html

求一个二进制搜索算法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值