当前位置:网站首页>二分法查找
二分法查找
2022-07-17 00:06:00 【别Null.了】
目录
二分法查找
介绍二分法查找
二分法查找,也称为折半法,是一种在有序数组中查找特定元素的搜索算法。
时间复杂度:
最坏情况查找结果为最后一个元素(或者第一个元素)T(n)=T(n/2)+O(1)所以T(n)=O(log2n)
最好情况O(1)所要查找的元素即为中间元素(奇数长度数列的正中间,偶数长度数列的中间靠左的元素)
空间复杂度 S(n)=logn
二分法的查找思路
1、首先,从数组的中间元素开始搜索,如果该元素正好是目标元素,则搜索过程结束,否则执行下一步。
2、如果目标元素大于/小于中间元素,则在数组大于/小于中间元素的那一半区域查找,然后重复步骤一的操作,进行递归查找。
3、如果某一步数组为空,则表示找不到目标元素。
二分法有关示例
解题思路
例如,在有序数据 [26,30,45,55,60,61,62,65,70,78,99] 中查找 55 的过程

代码实现
需要注意的是:在使用二分法之前要将数组进行排序,二分法针对的是有序数组。
非递归算法
function binarySearch(arr,key){
var low=0; //数组最小索引值
var high=arr.length-1; //数组最大索引值
while(low<=high){
var mid=Math.floor((low+high)/2);
if(key==arr[mid]){
return mid;
}else if(key>arr[mid]){
low=mid+1;
}else{
high=mid-1;
}
}
return -1; //low>high的情况,这种情况下key的值大于arr中最大的元素值或者key的值小于arr中最小的元素值
}
实现结果

递归算法
function binarySearch(arr,low,high,key){
if(low>high){
return -1;
}
var mid=Math.floor((low+high)/2);
if(key==arr[mid]){
return mid;
}else if(key<arr[mid]){
high=mid-1;
return binarySearch(arr,low,high,key);
}else{
low=mid+1;
return binarySearch(arr,low,high,key);
} 实现结果

边栏推荐
- C Programming Language(2nd Edition)--读书笔记--1.5
- 数组操作——判断、去重、合并、展开
- Day11 serializer
- (10)文件包含
- Initial flask
- object-fit:cover; It doesn't work in the applet. How to deal with the deformation of the applet image
- JS intercepts the first few digits of the string or the last few digits of the string
- Day14 view set and route
- Eye of depth III - (3)] mathematics: matrix eigenvalue and eigenvector 1
- Eye of depth III - (7)] mathematics: application of SVD decomposition
猜你喜欢

uni-app微信小程序——商城(3)——商城主页

MoveIt2——4.机器人模型和机器人状态
![Eye of depth III - (4, 5)] mathematics: matrix eigenvalues and eigenvectors 2](/img/fc/7a4bffc642d82fcfaf3be80bcc39fc.png)
Eye of depth III - (4, 5)] mathematics: matrix eigenvalues and eigenvectors 2

The C Programming Language (2nd)--笔记--1.6

1. Internet foundation

深度之眼三——(3)】 數學:矩陣特征值與特征向量1

Quine injection of SQL injection
![The eye of Depth III - - (3)] Mathematics: Matrix eigenvalue and eigenvector 1](/img/81/7ad44da70eaf1d92b126c567766577.png)
The eye of Depth III - - (3)] Mathematics: Matrix eigenvalue and eigenvector 1

PCRE bypasses regular
![[SWPU 2019] network TTL encryption and some related knowledge](/img/c7/8a4b6e7808be9189e76563848b359d.png)
[SWPU 2019] network TTL encryption and some related knowledge
随机推荐
(七)流程控制
jsx 编译
cmake的基本使用
Eye of depth III - (3)] mathematics: matrix eigenvalue and eigenvector 1
“我的”Bug大全(转载)
Eye of depth III - (7)] mathematics: application of SVD decomposition
Collection and summary of penetration test information
JS replaces a character in the string, and JS modifies the specified character in the string
uni-app微信小程序——商城(6)——我的主页
智能指针(shared_ptr、unique_ptr、weak_ptr)
Day13 mixed view base class
The eye of Depth III - - (3)] Mathematics: Matrix eigenvalue and eigenvector 1
时间戳转化时间
ModelArts-图像分类and物体检测
2022年暑假ACM热身练习2(总结)
自己封装的风格化的开关卡片组件
Day05-Cookie,Session,Csrf
Oracle database architecture
CTF CRYPTO RSA入门刷题
Maker-鸿蒙应用开发培训04