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 and has n elements. Return the index of target, or -1 if it isn't present. Keep lo and hi bounds, check the middle each step, and discard the half that can't contain the target.
中文
二分查找
数组是已排序的,有 n 个元素。返回 target 的下标,如果不存在就返回 -1。维护 lo 和 hi 边界,每一步检查中间元素,丢弃不可能包含目标的那一半。
Complete int binary_search(const int *a, int n, int target) for a sorted array of length n: return the index of target, or -1 if absent. · 为长度为 n 的已排序数组完成 int binary_search(const int *a, int n, int target):返回 target 的下标,不存在则返回 -1。
Click Run to see the output here. · 点击“运行”查看此处输出。