- vừa được xem lúc

Blog#124: 🌸Introducing Binary Search: The Most Efficient Way to Find Data in JavaScript🌸

0 0 17

Người đăng: NGUYỄN ANH TUẤN

Theo Viblo Asia

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.


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.

Ref

Bình luận

Bài viết tương tự

- vừa được xem lúc

Giới thiệu Typescript - Sự khác nhau giữa Typescript và Javascript

Typescript là gì. TypeScript là một ngôn ngữ giúp cung cấp quy mô lớn hơn so với JavaScript.

0 0 500

- vừa được xem lúc

Bạn đã biết các tips này khi làm việc với chuỗi trong JavaScript chưa ?

Hi xin chào các bạn, tiếp tục chuỗi chủ đề về cái thằng JavaScript này, hôm nay mình sẽ giới thiệu cho các bạn một số thủ thuật hay ho khi làm việc với chuỗi trong JavaScript có thể bạn đã hoặc chưa từng dùng. Cụ thể như nào thì hãy cùng mình tìm hiểu trong bài viết này nhé (go).

0 0 414

- vừa được xem lúc

Một số phương thức với object trong Javascript

Trong Javascript có hỗ trợ các loại dữ liệu cơ bản là giống với hầu hết những ngôn ngữ lập trình khác. Bài viết này mình sẽ giới thiệu về Object và một số phương thức thường dùng với nó.

0 0 136

- vừa được xem lúc

Tìm hiểu về thư viện axios

Giới thiệu. Axios là gì? Axios là một thư viện HTTP Client dựa trên Promise.

0 0 117

- vừa được xem lúc

Imports và Exports trong JavaScript ES6

. Giới thiệu. ES6 cung cấp cho chúng ta import (nhập), export (xuất) các functions, biến từ module này sang module khác và sử dụng nó trong các file khác.

0 0 93

- vừa được xem lúc

Bài toán đọc số thành chữ (phần 2) - Hoàn chỉnh chương trình dưới 100 dòng code

Tiếp tục bài viết còn dang dở ở phần trước Phân tích bài toán đọc số thành chữ (phần 1) - Phân tích đề và những mảnh ghép đầu tiên. Bạn nào chưa đọc thì có thể xem ở link trên trước nhé.

0 0 229