# 二分查找 ```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)