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.
binarySearch([1, 2, 3, 6, 9, 11], 6); // 3binarySearch([1, 2, 3, 12, 16, 14], 5); // -1
Binary search is a search algorithm that can efficiently determine if a sorted array of integers contain a specific number. The algorithm repeatedly divides the input array into half until the target element is found, thereby decreasing the search space by half each step. It is a significant improvement versus linear search.
Here is a quick explanation of how binary search works on an array that is already sorted: