Introduction
Binary Search is a fundamental algorithm in computer science used for finding an element in a sorted array. It operates by repeatedly dividing the search interval in half. If the value of the search key is less than the item in the middle of the interval, the algorithm narrows the interval to the lower half. Otherwise, it narrows it to the upper half. This process continues until the value is found or the interval is empty.
Key Concepts
- Sorted Array: Binary Search requires the array to be sorted.
- Divide and Conquer: The algorithm divides the problem into smaller subproblems and solves them recursively.
- Logarithmic Time Complexity: Binary Search operates in O(log n) time, making it very efficient for large datasets.
Basic Binary Search Algorithm
Pseudocode
function binarySearch(arr, target): low = 0 high = length(arr) - 1 while low <= high: mid = low + (high - low) / 2 if arr[mid] == target: return mid else if arr[mid] < target: low = mid + 1 else: high = mid - 1 return -1
Explanation
- Initialization: Set
low
to 0 andhigh
to the last index of the array. - Loop: Continue the loop while
low
is less than or equal tohigh
. - Midpoint Calculation: Calculate the midpoint
mid
of the current interval. - Comparison:
- If
arr[mid]
equals the target, returnmid
. - If
arr[mid]
is less than the target, adjustlow
tomid + 1
. - If
arr[mid]
is greater than the target, adjusthigh
tomid - 1
.
- If
- Termination: If the target is not found, return -1.
Example
def binary_search(arr, target): low = 0 high = len(arr) - 1 while low <= high: mid = low + (high - low) // 2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return -1 # Example usage arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] target = 7 print(binary_search(arr, target)) # Output: 6
Variants of Binary Search
- Binary Search for First Occurrence
To find the first occurrence of a target value in a sorted array:
def binary_search_first_occurrence(arr, target): low = 0 high = len(arr) - 1 result = -1 while low <= high: mid = low + (high - low) // 2 if arr[mid] == target: result = mid high = mid - 1 # Continue searching in the left half elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return result # Example usage arr = [1, 2, 2, 2, 3, 4, 5] target = 2 print(binary_search_first_occurrence(arr, target)) # Output: 1
- Binary Search for Last Occurrence
To find the last occurrence of a target value in a sorted array:
def binary_search_last_occurrence(arr, target): low = 0 high = len(arr) - 1 result = -1 while low <= high: mid = low + (high - low) // 2 if arr[mid] == target: result = mid low = mid + 1 # Continue searching in the right half elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return result # Example usage arr = [1, 2, 2, 2, 3, 4, 5] target = 2 print(binary_search_last_occurrence(arr, target)) # Output: 3
- Binary Search for Insertion Point
To find the insertion point for a target value in a sorted array:
def binary_search_insertion_point(arr, target): low = 0 high = len(arr) - 1 while low <= high: mid = low + (high - low) // 2 if arr[mid] < target: low = mid + 1 else: high = mid - 1 return low # Example usage arr = [1, 2, 4, 5, 6] target = 3 print(binary_search_insertion_point(arr, target)) # Output: 2
Practical Exercises
Exercise 1: Basic Binary Search
Implement a binary search function that returns the index of a target value in a sorted array, or -1 if the target is not found.
def binary_search(arr, target): # Your code here pass # Test cases print(binary_search([1, 2, 3, 4, 5], 3)) # Expected output: 2 print(binary_search([1, 2, 3, 4, 5], 6)) # Expected output: -1
Solution
def binary_search(arr, target): low = 0 high = len(arr) - 1 while low <= high: mid = low + (high - low) // 2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return -1 # Test cases print(binary_search([1, 2, 3, 4, 5], 3)) # Output: 2 print(binary_search([1, 2, 3, 4, 5], 6)) # Output: -1
Exercise 2: First Occurrence Binary Search
Implement a binary search function that returns the index of the first occurrence of a target value in a sorted array, or -1 if the target is not found.
def binary_search_first_occurrence(arr, target): # Your code here pass # Test cases print(binary_search_first_occurrence([1, 2, 2, 2, 3, 4, 5], 2)) # Expected output: 1 print(binary_search_first_occurrence([1, 2, 3, 4, 5], 6)) # Expected output: -1
Solution
def binary_search_first_occurrence(arr, target): low = 0 high = len(arr) - 1 result = -1 while low <= high: mid = low + (high - low) // 2 if arr[mid] == target: result = mid high = mid - 1 # Continue searching in the left half elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return result # Test cases print(binary_search_first_occurrence([1, 2, 2, 2, 3, 4, 5], 2)) # Output: 1 print(binary_search_first_occurrence([1, 2, 3, 4, 5], 6)) # Output: -1
Conclusion
Binary Search is a powerful algorithm for searching in sorted arrays with a logarithmic time complexity. Understanding its basic implementation and variants is crucial for solving a wide range of computational problems efficiently. The exercises provided will help reinforce the concepts and ensure a solid grasp of binary search techniques.
Advanced Algorithms
Module 1: Introduction to Advanced Algorithms
Module 2: Optimization Algorithms
Module 3: Graph Algorithms
- Graph Representation
- Graph Search: BFS and DFS
- Shortest Path Algorithms
- Maximum Flow Algorithms
- Graph Matching Algorithms
Module 4: Search and Sorting Algorithms
Module 5: Machine Learning Algorithms
- Introduction to Machine Learning
- Classification Algorithms
- Regression Algorithms
- Neural Networks and Deep Learning
- Clustering Algorithms
Module 6: Case Studies and Applications
- Optimization in Industry
- Graph Applications in Social Networks
- Search and Sorting in Large Data Volumes
- Machine Learning Applications in Real Life