Binary Search is a divide-and-conquer search algorithm that finds the position of a target value within a sorted array.
It works by repeatedly dividing the search interval in half, making it significantly faster than linear search with a time complexity of O(log n).
The basic steps of binary search are:
Here's a simple implementation of binary search:
This function returns the index of the target value if found, or -1 if not found.
Binary search is particularly useful for:
Understand the fundamental concepts of binary search and its importance in efficient searching algorithms.
Binary Search is a divide-and-conquer search algorithm that finds the position of a target value within a sorted array.
It works by repeatedly dividing the search interval in half, comparing the target value with the middle element, and eliminating half of the remaining elements in each step.
Binary search is like finding a name in a phone book. Instead of checking each page, you open to the middle, see if your target comes before or after, and eliminate half the book with each step.
The array must be sorted in ascending or descending order for binary search to work correctly.
Repeatedly divides the search space in half, eliminating half of the remaining elements in each step.
O(log n) time complexity, making it very efficient for large datasets compared to linear search's O(n).
Uses comparisons to determine which half to search next, making it adaptable to different data types.
Calculate the middle index of the current search range.
Compare the target value with the middle element.
If equal, return the middle index as the target is found.
If target is less, search left half; if greater, search right half.
Repeat until target is found or search space is empty.
Here's a simple implementation of binary search:
1234567891011121314151617181920212223242526function binarySearch(arr, target) { let left = 0; let right = arr.length - 1; while (left <= right) { // Find the middle index const mid = Math.floor((left + right) / 2); // Check if target is found if (arr[mid] === target) { return mid; } // If target is less than mid, search left half if (arr[mid] > target) { right = mid - 1; } // If target is greater than mid, search right half else { left = mid + 1; } } // Target not found return -1;}
Note: The condition left >= right
ensures we check all elements. When left > right
, the search space is empty, meaning the target is not in the array.
O(log n), where n is the number of elements in the array.
With each step, we eliminate half of the remaining elements, leading to a logarithmic time complexity. For example, in an array of 1,000,000 elements, binary search would require at most 20 comparisons.
O(1) for iterative implementation, O(log n) for recursive implementation.
The iterative implementation uses a constant amount of extra space regardless of input size. The recursive implementation uses space proportional to the depth of the recursion, which is log n.
Binary Search is a divide-and-conquer search algorithm that finds the position of a target value within a sorted array.
It works by repeatedly dividing the search interval in half, making it significantly faster than linear search with a time complexity of O(log n).
The basic steps of binary search are:
Here's a simple implementation of binary search:
This function returns the index of the target value if found, or -1 if not found.
Binary search is particularly useful for:
Understand the fundamental concepts of binary search and its importance in efficient searching algorithms.
Binary Search is a divide-and-conquer search algorithm that finds the position of a target value within a sorted array.
It works by repeatedly dividing the search interval in half, comparing the target value with the middle element, and eliminating half of the remaining elements in each step.
Binary search is like finding a name in a phone book. Instead of checking each page, you open to the middle, see if your target comes before or after, and eliminate half the book with each step.
The array must be sorted in ascending or descending order for binary search to work correctly.
Repeatedly divides the search space in half, eliminating half of the remaining elements in each step.
O(log n) time complexity, making it very efficient for large datasets compared to linear search's O(n).
Uses comparisons to determine which half to search next, making it adaptable to different data types.
Calculate the middle index of the current search range.
Compare the target value with the middle element.
If equal, return the middle index as the target is found.
If target is less, search left half; if greater, search right half.
Repeat until target is found or search space is empty.
Here's a simple implementation of binary search:
1234567891011121314151617181920212223242526function binarySearch(arr, target) { let left = 0; let right = arr.length - 1; while (left <= right) { // Find the middle index const mid = Math.floor((left + right) / 2); // Check if target is found if (arr[mid] === target) { return mid; } // If target is less than mid, search left half if (arr[mid] > target) { right = mid - 1; } // If target is greater than mid, search right half else { left = mid + 1; } } // Target not found return -1;}
Note: The condition left >= right
ensures we check all elements. When left > right
, the search space is empty, meaning the target is not in the array.
O(log n), where n is the number of elements in the array.
With each step, we eliminate half of the remaining elements, leading to a logarithmic time complexity. For example, in an array of 1,000,000 elements, binary search would require at most 20 comparisons.
O(1) for iterative implementation, O(log n) for recursive implementation.
The iterative implementation uses a constant amount of extra space regardless of input size. The recursive implementation uses space proportional to the depth of the recursion, which is log n.