π Binary Search
Introductionβ
Binary Search is an efficient search algorithm that works by repeatedly dividing a sorted array in half to find a specific element.
Complexityβ
- Time: O(log n)
- Space: O(1) iterative, O(log n) recursive
How It Worksβ
- Compare the middle element of the array to the target
- If equal, the element is found
- If the target is less, search the left half
- If the target is greater, search the right half
- Repeat until found or the search space is empty
Implementationβ
Iterative Versionβ
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1 # Element not found
Recursive Versionβ
def binary_search_recursive(arr, target, left=0, right=None):
if right is None:
right = len(arr) - 1
if left > right:
return -1
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
return binary_search_recursive(arr, target, mid + 1, right)
else:
return binary_search_recursive(arr, target, left, mid - 1)
Usage Exampleβ
# Sorted array
arr = [1, 3, 5, 7, 9, 11, 13, 15]
target = 7
# Search
index = binary_search(arr, target)
print(f"Element {target} found at index: {index}") # Output: 3
Prerequisitesβ
β οΈ Important: The array must be sorted for binary search to work correctly.
Advantages and Disadvantagesβ
β Advantagesβ
- Very efficient for large arrays
- O(log n) time complexity
- Minimal memory usage in the iterative version
β Disadvantagesβ
- Requires a sorted array
- Not efficient for linked lists
- O(log n) space complexity in the recursive version