Hi, I'm Tuan, a Full-stack Web Developer from Tokyo 😊. Follow my blog to not miss out on useful and interesting articles in the future.
Write an article about Binary Search in Javascript in plain English for kids. The title of the article should be eye-catching and engaging to readers. Explanations should be concise, clear, and as visual as possible and visual example. Additionally, the article should include titles for each section in Markdown format.
What is Binary Search?
Have you ever wanted to find something quickly and easily? Binary Search is a way of quickly finding something in a list of items. It's like a treasure hunt, but with numbers instead of clues!
How Does Binary Search Work?
Binary Search works by taking a list of items and dividing it in half. It then looks at the number in the middle of the list. If that number is the one we are looking for, then we have found it! But if the number is not the one we are looking for, then we divide the list in half again and look at the number in the middle. We keep doing this until we find the number we are looking for.
What Does Binary Search Look Like in Code?
Let's say we have a list of numbers called numbers
that looks like this:
[1, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Here is a simple example of Binary Search written in Javascript:
function binarySearch(numbers, target) {
// Set the start and end of our search
let start = 0;
let end = numbers.length - 1;
// Keep searching until we find the target
while (start <= end) {
// Find the middle of our search
let middle = Math.floor((start + end) / 2);
// Check if the middle is the target
if (numbers[middle] === target) {
return middle;
}
// If the middle is not the target, then
// check if it is greater or less than
// the target and adjust our search accordingly
if (numbers[middle] < target) {
start = middle + 1;
} else {
end = middle - 1;
}
}
// If we don't find the target, then return -1
return -1;
}
Try It Out!
Now that you know how Binary Search works, why not try it out for yourself? Here is an example of how you can use the code above to find the number 8 in our list of numbers:
let numbers = [1, 2, 4, 6, 8, 10, 12, 14, 16, 18];
let target = 8;
let index = binarySearch(numbers, target);
console.log(index); // 4
As you can see, the index of the number 8 is 4. That means it is the 5th number in our list!
Other Example
// An array of words in a dictionary, sorted alphabetically
const dictionaryWords = [
{ index: 1, word: "book" },
{ index: 3, word: "computer" },
{ index: 7, word: "dictionary" },
{ index: 9, word: "elephant" },
{ index: 80, word: "flower" },
];
function binarySearch(list, item, filterCondition = (e) => e) {
// Get the middle item of the list
const middle = Math.floor(list.length / 2);
const middleItem = filterCondition(list[middle]);
// If the item is the middle item, return it
if (item === middleItem) {
return list[middle];
}
// If the item is less than the middle item, search the first half of the list
if (item < middleItem) {
return binarySearch(list.slice(0, middle), item, filterCondition);
}
// If the item is greater than the middle item, search the second half of the list
if (item > middleItem) {
return binarySearch(list.slice(middle + 1), item, filterCondition);
}
// If the item is not found, return -1
return -1;
}
const word = binarySearch(dictionaryWords, 3, (e) => e.index);
console.log(word); // { index: 7, word: 'dictionary' }
Conclusion
Binary Search is a great way to quickly find something in a list of items. It is fast and efficient and can be used to solve many different types of problems. Now that you know how Binary Search works, why not give it a try?
And Finally
As always, I hope you enjoyed this article and learned something new. Thank you and see you in the next articles!
If you liked this article, please give me a like and subscribe to support me. Thank you. 😊
The main goal of this article is to help you improve your English level. I will use Simple English to introduce to you the concepts related to software development. In terms of IT knowledge, it might have been explained better and more clearly on the internet, but remember that the main target of this article is still to LEARN ENGLISH.