Binary search
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
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.
Complete binarySearch(int[] a, int target) for a sorted array a: return the index of target, or -1 if absent. Use binary search.
Click Run to see the output here.