← Back
Implement binary search algorithm.
Given an array of integers, do binary search on it.
Algorithm
Binary Search (array must be sorted):
Set low = 0 and high = array length - 1.
While low ≤ high:
Calculate mid = Math.floor((low + high) / 2).
If array[mid] equals target, return mid or indicate success.
If array[mid] < target, set low = mid + 1.
If array[mid] > target, set high = mid - 1.
If not found, indicate failure.
Time Complexity
Binary Search:
O(log n), where n is the number of elements in the sorted array.
Array Elements
Enter numbers separated by commas
Target Value
Binary Search