Learn everything you need to know about Binary Search in JavaScript. This guide covers everything you need to know about Binary Search from basics to advanced techniques with examples.
Suprasanna Ojha
Binary search is a fundamental algorithm in computer science that efficiently locates a target value within a sorted array. It's like a high-tech version of the "higher or lower" guessing game. In this blog post, we'll dive deep into binary search, exploring its concepts, implementation in JavaScript, and real-world applications.
Binary search is a search algorithm that finds the position of a target value within a sorted array. It works 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, narrow the interval to the lower half. Otherwise, narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.
Imagine you're looking for a specific page in a book:
That's essentially how binary search works!
Here's a simple pseudo-code representation of the binary search algorithm:
function binarySearch(array, target):
left = 0
right = length of array - 1
while left <= right:
middle = (left + right) / 2
if array[middle] == target:
return middle
else if array[middle] < target:
left = middle + 1
else:
right = middle - 1
return -1 // Target not found
Now, let's implement this algorithm in JavaScript:
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid; // Target found, return its index
} else if (arr[mid] < target) {
left = mid + 1; // Target is in the right half
} else {
right = mid - 1; // Target is in the left half
}
}
return -1; // Target not found
}
Let's explore some original examples to better understand how binary search works in different scenarios.
Let's start with a simple example of finding a number in a sorted array.
const sortedNumbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47];
function findNumber(numbers, target) {
const result = binarySearch(numbers, target);
if (result !== -1) {
console.log(`The number ${target} is found at index ${result}.`);
} else {
console.log(`The number ${target} is not in the array.`);
}
}
findNumber(sortedNumbers, 23); // Output: The number 23 is found at index 8.
findNumber(sortedNumbers, 25); // Output: The number 25 is not in the array.
In this example, we use binary search to quickly find a number in a sorted array of prime numbers.
Let's create a more real-world example: finding a book in a sorted library catalog.
class Book {
constructor(title, isbn) {
this.title = title;
this.isbn = isbn;
}
}
const libraryCatalog = [
new Book("Algorithms", "9780262033848"),
new Book("Clean Code", "9780132350884"),
new Book("Design Patterns", "9780201633610"),
new Book("JavaScript: The Good Parts", "9780596517748"),
new Book("You Don't Know JS", "9781491924464")
];
function findBook(catalog, targetIsbn) {
const compare = (book, target) => {
if (book.isbn === target) return 0;
return book.isbn < target ? -1 : 1;
};
let left = 0;
let right = catalog.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
const comparison = compare(catalog[mid], targetIsbn);
if (comparison === 0) {
return catalog[mid];
} else if (comparison < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return null;
}
const searchIsbn = "9780596517748";
const foundBook = findBook(libraryCatalog, searchIsbn);
if (foundBook) {
console.log(`Book found: ${foundBook.title} (ISBN: ${foundBook.isbn})`);
} else {
console.log(`No book found with ISBN ${searchIsbn}`);
}
In this example, we use binary search to find a book in a sorted library catalog using its ISBN. This demonstrates how binary search can be applied to more complex data structures.
Let's implement a number guessing game that uses binary search to guess the player's number efficiently.
function guessingGame() {
const min = 1;
const max = 100;
let guesses = 0;
console.log("Think of a number between 1 and 100, and I'll guess it!");
function makeGuess(low, high) {
if (low > high) {
console.log("You must have changed your number! Let's start over.");
return;
}
const guess = Math.floor((low + high) / 2);
guesses++;
console.log(`Is your number ${guess}? (Type 'h' for higher, 'l' for lower, or 'c' for correct)`);
// In a real implementation, we'd get user input here.
// For this example, let's assume the number is 73.
const userNumber = 73;
if (guess === userNumber) {
console.log(`I guessed your number ${guess} in ${guesses} guesses!`);
} else if (guess < userNumber) {
console.log("You said it's higher. I'll guess again.");
makeGuess(guess + 1, high);
} else {
console.log("You said it's lower. I'll guess again.");
makeGuess(low, guess - 1);
}
}
makeGuess(min, max);
}
guessingGame();
This example demonstrates how binary search can be used in an interactive scenario, efficiently guessing a number between 1 and 100 in no more than 7 guesses.
One of the key advantages of binary search is its efficiency. The time complexity of binary search is O(log n), where n is the number of elements in the array. This means that even for very large datasets, binary search can find the target quickly.
For example:
This logarithmic time complexity makes binary search significantly faster than linear search (O(n)) for large datasets.
Binary search is ideal when:
It's particularly useful in scenarios like:
(left + right) / 2 can cause issues for very large arrays. A safer alternative is left + (right - left) / 2.Binary search is a powerful algorithm that dramatically improves search efficiency in sorted datasets. By repeatedly dividing the search interval in half, it achieves logarithmic time complexity, making it invaluable for working with large datasets.
As you've seen through our examples, binary search can be applied to various scenarios beyond simple number searches. Whether you're developing a search function for a large database, creating a game, or optimizing any search process on sorted data, binary search is a crucial tool in your programming toolkit.
Remember, the key to mastering binary search is practice. Try implementing it in different scenarios, and soon you'll find yourself reaching for this efficient algorithm whenever you need to search through sorted data.
Happy coding, and may your searches always be logarithmic!