Binary Search

语言

Implement a function that performs binary search on an array of numbers. The function should take in a sorted array of integers and a target integer to find. It returns the index of the target element or -1, if the target element doesn't exist in the array.

Examples

binarySearch([1, 2, 3, 6, 9, 11], 6); // 3
binarySearch([1, 2, 3, 12, 14, 16], 5); // -1

Hints

加载编辑器

    Binary Search | 算法面试题及解决方案