Binary search · 二分查找
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Binary search
The array is sorted. Return the index of target, or -1 if it isn't there. Binary search halves the range each step: check the middle element, then keep looking in just the left or right half. Far faster than scanning every element.
中文
二分查找
数组是已排序的。返回 target 的下标,如果不存在就返回 -1。二分查找每一步把范围减半:检查中间元素,然后只在左半或右半继续查找。比逐个扫描快得多。
Complete binarySearch(int[] a, int target) for a sorted array a: return the index of target, or -1 if absent. Use binary search. · 为已排序数组 a 完成 binarySearch(int[] a, int target):返回 target 的下标,不存在则返回 -1。使用二分查找。
Click Run to see the output here. · 点击“运行”查看此处输出。