数组中的二分查找
如何仅使用数组来实现二分搜索?
How would I implement a binary search using just an array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何仅使用数组来实现二分搜索?
How would I implement a binary search using just an array?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(11)
确保您的数组已排序,因为这是二分搜索的关键。
任何索引/随机访问数据结构都可以进行二分搜索。 因此,当您说使用“只是一个数组”时,我会说数组是采用二分搜索的最基本/常见的数据结构。
您可以递归(最简单)或迭代地执行此操作。 二分搜索的时间复杂度为 O(log N),这比以 O(N) 检查每个元素的线性搜索要快得多。 以下是维基百科:二分搜索算法中的一些示例:
递归:
迭代:
Ensure that your array is sorted since this is the crux of a binary search.
Any indexed/random-access data structure can be binary searched. So when you say using "just an array", I would say arrays are the most basic/common data structure that a binary search is employed on.
You can do it recursively (easiest) or iteratively. Time complexity of a binary search is O(log N) which is considerably faster than a linear search of checking each element at O(N). Here are some examples from Wikipedia: Binary Search Algorithm:
Recursive:
Iterative:
Javascript 中的二分搜索(ES6)
(如果有人需要)
自下而上:
递归:
Binary Search in Javascript (ES6)
(If anyone needs)
Bottom-up:
Recursion:
仅使用数组实现二分搜索
二分搜索是搜索数组中元素的优化解决方案,因为它通过以下三种方式减少搜索时间
如果不满足上述任何一种情况,则该元素不存在于数组中。
二分查找的好处
算法步骤
第1步:使用
最低索引和最高索引
的下限计算中间索引
一个数组。第 2 步:将要搜索的元素与
中间索引
处存在的元素进行比较第 3 步: 如果第 2 步 不满足,则检查中间元素左侧的所有元素。 为此,等于
高索引 = 中索引 - 1
步骤 4: 如果不满足步骤 3,则检查所有元素到中间元素的右侧。 为此,等于
低索引 = 中索引 + 1
如果不满足任何情况,则等于
低索引=中索引+ 1
,然后返回-1,这意味着要搜索的元素不存在于整个数组中。代码
递归代码也可以为二分查找编写。 两者(迭代和递归)都采用
O(logn)
作为时间复杂度,但当要考虑空间复杂度时,该解决方案的迭代方法将获胜,因为它需要O(1)
而对于递归算法,函数调用堆栈中将使用三个函数调用,因此空间复杂度等于O(logn)
。 下面是递归解决方案。Implementing a binary search using just an array
Binary search is an optimized solution for searching an element in an array as it reduces search time by following three ways
If any of the above cases is not satisfied then such element is not present in an array.
Benefits of binary search
Algorithm Steps
Step 1: Calculate the
mid index
using the floor oflowest index and highest index
in an array.Step 2: Compare the element to be searched with the element present at the
middle index
Step 3: If step 2 is not satisfied, then check for all element to the left of middle element. To do so equate
high index = mid index - 1
Step 4: If step 3 is not satisfied, then check for all elements to the right of the middle element. To do so equate
low index = mid index + 1
if no case is satisfied then return -1 which means that element to be searched is not present in the whole array.
Code
Recursive code can also be written for the binary search. Both (iterative and recursive) take
O(logn)
as the time complexity but when space complexity is to be considered then iterative approach for this solution will win as it takesO(1)
whereas for recursive algo, three functions calls will be used in the function call stack and hence space complexity becomes equal toO(logn)
. Below is the recursive solution.用Java实现二分查找算法
将起始索引(低位)设置为 0,将结束索引(高位)设置为数组长度减 1。
当起始索引小于或等于结束索引时:
在这里发现全面的解决方案 https://github.com/Amir36036/ds/blob/array/src/main/java/org/ds/array/BinarySearch.java
Implementing Binary Search Algorithm in Java
Set the start index (low) to 0 and the end index (high) to the length of the array minus 1.
While the start index is less than or equal to the end index:
Discover Comprehensive Solution Here https://github.com/Amir36036/ds/blob/array/src/main/java/org/ds/array/BinarySearch.java
这取决于您的数组中是否重复某个元素,以及您是否关心多个结果。 我在这个实现中有两种方法。 其中一个仅返回第一个发现,而另一个则返回该键的所有发现。
有关更多信息,请访问 https://github.com /m-vahidalizadeh/foundations/blob/master/src/algorithms/BinarySearchExample.java。 我希望它有帮助。
It depends if you have repetition of one element in your array or no and if you care about multiple findings or not. I have two methods in this implementation. One of them returns only first finding, but the other one returns all findings of the key.
For more information, please visit https://github.com/m-vahidalizadeh/foundations/blob/master/src/algorithms/BinarySearchExample.java. I hope it helps.
单一比较版本快速简洁
The single comparison version is fast and concise
用Java实现了下面的代码,简单快速
/**
* 使用递归进行二分查找
* @作者阿沙达
*
*/
公共类 BinSearch {
Did implement below code in Java,simple and fast
/**
* Binary Search using Recursion
* @author asharda
*
*/
public class BinSearch {
这是Python编程语言的简单解决方案:
过程如下:
Here is simple solution in python programming language:
Here is the process :
二分搜索的短循环:
想法正在替换:
如果观察,则具有更多默契(并且可能更少的计算需求)
前者相等:
所以如果 left、mid、right 变量按顺序排列,我们可以对所有变量进行寻址,在 C 指针意义上分别抛出 &mid[-1,0,1] :
现在我们得到循环体,这样我们就可以构造二进制搜索:
之后我们只是:
使用语义,
所以在一些更人性化的伪代码中,javascript函数等效:
对于js sintax,我们需要使用 q={'-1':0,1:nums.length-1} 其中 q[ 的左侧名称-1],q[0] 的中间,q[1] 的右边,或者所有 3 的 q 是 q[dir]
或从 0 开始的数组索引相同:
我们可以使用 p=[0,,nums.length- 1] 其中左侧是 p[0] 的昵称,中间是 p[1] 的昵称,右侧是 p[2] 的昵称,这三个昵称是 p[1+dir]
。 :)
short loop for binary search:
idea is replacing:
with more tacit(and possible less computation hungry) if observe
former is equal:
so if left,mid,right vars situated sequentially we can address to all of them throw &mid[-1,0,1 respectively] in C pointer sense :
now we get body of loop so we can construct binary search:
after while we just:
with semantic that
so in some more human pseudocode javascript function equivalent:
for js sintax we need use q={'-1':0,1:nums.length-1} where left name for q[-1], mid for q[0] and right for q[1] or for q for all 3 is q[dir]
or the same for array indexing from 0:
we can use p=[0,,nums.length-1] where left is nikname for p[0], mid for p[1] and right for p[2] which is for all 3 of them is p[1+dir]
. :)
假设数组已排序,这是一个运行时复杂度为 O(log n) 的 Python 答案:
Assuming the array is sorted, here is a Pythonic answer with O(log n) runtime complexity:
注意,数组需要排序!
我认为这是一个简单的方法,仅使用值来查找和数组。
递归:
迭代:
都很容易阅读,没有高低索引和 n 的对数,因为它每次都会将数组减半
Note that the array neeed to be sorted!
I think this is a simple one, using just value to look for and the array.
Recursive:
Iterative:
all easy to read, no high and low indexes and log of n because it halves the array every time