ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 二分查找 ```javascript let arr = [10, 20, 30, 40, 55, 66, 77, 88]; // 需要从小到大排序 function search(list, target) { let start = 0; let end = list.length-1; let middle; let element; while(start <= end) { middle = Math.floor((start + end)/2); element = list[middle]; if (element === target) { return middle; } else if (target < element) { end = middle - 1; } else { start = middle + 1; } } return -1; } console.log(search(arr, 30)) // 2 ``` - [JavaScript二分法寻找数组元素](https://www.bilibili.com/video/BV1iJ411N7Hw)