Okay, for this first question, I'd like you to implement a binary search algorithm. The binary search algorithm is a search algorithm that finds the position of a target value within a sorted array. It works by repeatedly dividing the search interval in half. If the middle element is the target, then the index is returned. If the target is less than the middle element, then the search continues in the left half. If the target is greater than the middle element, then the search continues in the right half. This process repeats until the target is found or the interval is empty.
Write a function binarySearch(arr, target)
that takes a sorted array arr
and a target value target
as input, and returns the index of the target value in the array if it exists, or -1 if it does not exist.
Here are some examples to illustrate the expected behavior:
binarySearch([2, 5, 7, 8, 11, 12], 13)
should return -1binarySearch([2, 5, 7, 8, 11, 12], 12)
should return 5binarySearch([2, 5, 7, 8, 11, 12], 0)
should return -1binarySearch([1, 2, 3, 4, 5, 6], 4)
should return 3binarySearch([-1, 0, 3, 5, 9, 12], 9)
should return 4binarySearch([-1, 0, 3, 5, 9, 12], 2)
should return -1Binary search is an efficient algorithm for finding a target value within a sorted array. It works by repeatedly dividing the search interval in half. If the middle element is the target, the search is successful. If the target is less than the middle element, the search continues in the left half. If the target is greater than the middle element, the search continues in the right half. This process continues until the target is found or the search interval is empty.
While binary search is already an efficient approach, let's consider a less optimal, brute-force perspective, not because we would use it, but to highlight the improvements binary search provides. Imagine we simply iterate through the sorted array, comparing each element to the target value.
python def linear_search(arr, target): for i in range(len(arr)): if arr[i] == target: return i return -1 # Target not found
This approach has a time complexity of O(n), where n is the number of elements in the array, because in the worst case, we might have to check every element. Its space complexity is O(1), as we only use a constant amount of extra space.
The optimal solution is the binary search algorithm itself.
python def binary_search(arr, target): left = 0 right = len(arr) - 1
while left <= right:
mid = (left + right) // 2 # Integer division to find the middle index
if arr[mid] == target:
return mid # Target found
elif arr[mid] < target:
left = mid + 1 # Search in the right half
else:
right = mid - 1 # Search in the left half
return -1 # Target not found
Binary search has a time complexity of O(log n), where n is the number of elements in the sorted array. This is because the search space is halved in each iteration.
Binary search has a space complexity of O(1), as it uses a constant amount of extra space regardless of the size of the input array.
The code is presented above in the 'Optimal Solution' section.