Binary search
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.
Click Run to see the output here. · Нажмите Запустить, чтобы увидеть результат здесь.