Binary Search
Clarify the problem:
- The problem requires implementing the binary search algorithm to find a target element in a sorted array.
- We are given a sorted array of integers and a target value.
- We need to determine if the target value is present in the array and return its index if found, or -1 if not found.
Analyze the problem:
- Input: A sorted array of integers and a target value.
- Output: The index of the target value in the array or -1 if not found.
- Constraints:
- The array can have up to 10^4 elements.
- The array is sorted in non-decreasing order.
- The target value is an integer.
Design an algorithm:
- We can solve this problem using the binary search algorithm.
- The algorithm can be implemented iteratively or recursively.
- In each iteration or recursive call, we will compare the target value with the middle element of the array.
- If the target value is equal to the middle element, we return its index.
- If the target value is less than the middle element, we search the left half of the array.
- If the target value is greater than the middle element, we search the right half of the array.
- We repeat this process until we find the target value or narrow down the search range to an empty interval.
Explain your approach:
- We will implement a function called
binarySearch
that takes the sorted array and the target value as input. - The function will initialize two pointers,
left
andright
, to the start and end indices of the array, respectively. - We will use a loop to continue the search until
left
becomes greater thanright
, indicating an empty search interval. - In each iteration, we calculate the
mid
index as the average ofleft
andright
. - We compare the target value with the element at index
mid
. - If they are equal, we return
mid
. - If the target value is less than the element at index
mid
, we updateright
tomid - 1
to search the left half. - If the target value is greater than the element at index
mid
, we updateleft
tomid + 1
to search the right half. - If we exhaust the loop without finding the target value, we return -1.
- We will implement a function called
Write clean and readable code:
pythondef binarySearch(nums, target): left = 0 right = len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid elif nums[mid] < target: left = mid + 1 else: right = mid - 1 return -1
Test your code:
- To test the code, we can create different test cases with various inputs.
- Test case 1:
python- Test case 2:
- Test case 3:
nums = [] target = 5 print(binarySearch(nums, target)) # Output: -1
Optimize if necessary:
- The provided implementation follows the optimal binary search algorithm.
- The time complexity of the binary search algorithm is O(log N), where N is the size of the input array.
- This is because the search range is divided in half in each iteration, resulting in a logarithmic time complexity.
- The space complexity is O(1) since we only use a constant amount of additional space.
Handle error cases:
- The code assumes that the input array is sorted in non-decreasing order.
- If the array is not sorted or the target value is not an integer, the code may not work correctly.
- We can handle these error cases by adding appropriate checks at the beginning of the
binarySearch
function to return -1.
Discuss complexity analysis:
- Time complexity: The binary search algorithm has a time complexity of O(log N), where N is the size of the input array. The search range is divided in half in each iteration, resulting in a logarithmic time complexity.
- Space complexity: The space complexity is O(1) since we only use a constant amount of additional space.
nums = [-1, 0, 3, 5, 9, 12]
target = 9
print(binarySearch(nums, target))
# Output: 4
python
nums = [-1, 0, 3, 5, 9, 12]
target = 2
print(binarySearch(nums, target))
# Output: -1
python