Goal: Implement an efficient search algorithm that runs in logarithmic time, O(log n).
The core idea of binary search is to halve the search space at each step.
left (pointing to the start of the array, index 0) and right (pointing to the end, arr.length - 1).left is not greater than right.middle = Math.floor((left + right) / 2).arr[middle] is equal to the target element, you’ve found it! Return middle.arr[middle] is less than the target, the target can only be in the right half of the array. Move the left boundary: left = middle + 1.arr[middle] is greater than the target, the target is in the left half. Move the right boundary: right = middle - 1.-1.const binarySearch = (arr, target) => {
// Initialize pointers to the beginning and end of the array.
let left = 0;
let right = arr.length - 1;
// Continue the loop as long as the left pointer is not to the right of the right pointer.
// This condition means the search space is not yet empty.
while (left <= right) {
// Find the middle index.
// Math.floor is used to round down to get a whole index.
const middle = Math.floor((left + right) / 2);
const guess = arr[middle];
// Compare the middle element with the target value.
if (guess === target) {
// Element found! Return its index.
return middle;
}
// If the middle element is less than the target,
// the target can only be in the right half of the array.
if (guess < target) {
// Move the left search boundary to the right of the middle.
left = middle + 1;
} else {
// Otherwise (the middle element is greater than the target),
// the target can only be in the left half.
// Move the right search boundary to the left of the middle.
right = middle - 1;
}
}
// If the loop has finished and the element was not found,
// it means it is not in the array.
return -1;
};Solution Analysis:
You need to implement a function binarySearch that takes a sorted array of numbers arr and a number target. The function should find the target in the array and return its index. If the element is not found, the function should return -1.
The key requirement is that the algorithm must have a logarithmic time complexity of O(log n), making it much more efficient than a linear search for large datasets.
// Finds an element in the middle
binarySearch([1, 2, 3, 4, 5, 6], 4); // 3
// Finds an element in an array with negative numbers
binarySearch([-1, 0, 3, 5, 9, 12], 9); // 4
// Element is missing
binarySearch([1, 2, 3, 4, 5], 7); // -1
// Empty array
binarySearch([], 5); // -1binarySearch.-1.Goal: Implement an efficient search algorithm that runs in logarithmic time, O(log n).
The core idea of binary search is to halve the search space at each step.
left (pointing to the start of the array, index 0) and right (pointing to the end, arr.length - 1).left is not greater than right.middle = Math.floor((left + right) / 2).arr[middle] is equal to the target element, you’ve found it! Return middle.arr[middle] is less than the target, the target can only be in the right half of the array. Move the left boundary: left = middle + 1.arr[middle] is greater than the target, the target is in the left half. Move the right boundary: right = middle - 1.-1.const binarySearch = (arr, target) => {
// Initialize pointers to the beginning and end of the array.
let left = 0;
let right = arr.length - 1;
// Continue the loop as long as the left pointer is not to the right of the right pointer.
// This condition means the search space is not yet empty.
while (left <= right) {
// Find the middle index.
// Math.floor is used to round down to get a whole index.
const middle = Math.floor((left + right) / 2);
const guess = arr[middle];
// Compare the middle element with the target value.
if (guess === target) {
// Element found! Return its index.
return middle;
}
// If the middle element is less than the target,
// the target can only be in the right half of the array.
if (guess < target) {
// Move the left search boundary to the right of the middle.
left = middle + 1;
} else {
// Otherwise (the middle element is greater than the target),
// the target can only be in the left half.
// Move the right search boundary to the left of the middle.
right = middle - 1;
}
}
// If the loop has finished and the element was not found,
// it means it is not in the array.
return -1;
};Solution Analysis:
You need to implement a function binarySearch that takes a sorted array of numbers arr and a number target. The function should find the target in the array and return its index. If the element is not found, the function should return -1.
The key requirement is that the algorithm must have a logarithmic time complexity of O(log n), making it much more efficient than a linear search for large datasets.
// Finds an element in the middle
binarySearch([1, 2, 3, 4, 5, 6], 4); // 3
// Finds an element in an array with negative numbers
binarySearch([-1, 0, 3, 5, 9, 12], 9); // 4
// Element is missing
binarySearch([1, 2, 3, 4, 5], 7); // -1
// Empty array
binarySearch([], 5); // -1binarySearch.-1.The code editor is intentionally hidden on mobile.
Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.
📖 Now: Study the task, think through the solution. Act like a strategist.
💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!