当前位置:网站首页>js二分法
js二分法
2022-06-12 17:58:00 【走出自闭的鸟儿】
二分法
对有序数组进行二分查找
- 递归实现:代码逻辑清晰
- 非递归:性能更好
- 时间复杂度O(logn)
function binarySearch(arr,target){
const length = arr.length
if(length<=0) return -1
let start = 0 //开始位置
let end = length-1 //结束位置
while(start <= end){
const mid = Math.floor((start+end)/2)
const midValue = arr[mid]
if(target<midValue){
// 目标值小于中间值,向左找
end = mid -1
}else if(target>midValue){
// 目标值大于中间值,向右找
start = mid +1
}else{
// 相等返回值
return mid
}
return -1
}
}
边栏推荐
- leetcode491 递增子序列
- 有源差分晶振原理图以及LV-PECL、LVDS、HCSL区别
- SQL游标(cursor)详细说明及内部循环使用示例
- Is it cost-effective to apply for Huagui sweet home term life insurance? What are the advantages of this product?
- The server time zone value ‘� й ��� ʱ ��‘ is unrecognized or represents more than one time zone. ......
- Codeforces Round #398 (Div. 2) D. Cartons of milk
- leetcode 300. 最长递增子序列
- grpc-swift入门
- Array sorts in the specified order
- 极限编程--根源分析实践
猜你喜欢

Use applet to quickly generate app in seven steps

ESP32-C3 ESP-IDF 配置smartconfig 和 sntp 获取网络时间

Arm64 stack backtracking

DRM driven MMAP detailed explanation: (I) preliminary knowledge

General differences between SQL server versions released by Microsoft in different periods so far, for reference

Deep interest evolution network for click through rate prediction

C#操作数据库增查业务数据值内容案例学校表

Vant3+ts H5 pages are nested into apps to communicate with native apps

NixOS 22.05安装过程记录

Extreme Programming -- Practice of root cause analysis
随机推荐
Leetcode491 increasing subsequence
PHP implementation of infinite classification tree (recursion and Optimization)
vant3 +ts 封装简易step进步器组件
有源差分晶振原理图以及LV-PECL、LVDS、HCSL区别
Getting started with grpc swift
用好IDE,研发效能提速100%
Bug record: data truncation: incorrect datetime value:
Nixos 22.05 installation process record
LCD parameter interpretation and calculation
Schedule update | 2022 Microsoft and Intel hacker song competition is in hot registration
566. 重塑矩阵
Detailed description of SQL cursor and example of internal recycling
Is it cost-effective to apply for Huagui sweet home term life insurance? What are the advantages of this product?
Channel Original
Research results of low code platform
MIPS general purpose register + instruction
系统设计之图状数据模型
leetcode 647. Palindrome substring
Click the list page of vant3+ts+pinia tab to enter the details. The tab on the details page is highlighted in the original position, and the refresh highlight is in the first item by default
Use of split method of string