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

Blog#125: 🌸バイナリサーチ:データを見つける最も効率的な方法🌸

0 0 9

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

Theo Viblo Asia

この記事の主な目的は、日本語レベルを上げるのを手伝うことです。ソフトウェア開発に関連する概念や知識なとを紹介するために簡単な日本語を使います。ITの知識に関しては、インターネット上でもっとよく説明されているかもしれませんが、この記事の主な目標はまだ日本語を学ぶことです。


こんにちは、私はトゥアンと申します。東京からフルスタックWeb開発者です。 将来の有用で面白い記事を見逃さないように、私のブログをフォローしてください。

あなたは今まで、すばやく簡単に何かを見つけたいと思ったことはありますか?バイナリサーチというのは、項目のリストの中からすばやく何かを見つける方法です。宝探しのようなものですが、手がかりの代わりに数字を使います!

バイナリサーチとはどのように機能しますか?

バイナリサーチは、アイテムのリストを半分に分けることで動作します。そして、リストの中央の数字を見ます。もしその数字が探している数字なら、見つけました!でも、もしその数字が探している数字でないなら、リストを再び半分に分けて、中央の数字を見ます。探している数字を見つけるまで、これを繰り返します。

どんなコードでバイナリサーチをするの?

「numbers」という数字のリストがあって、こんな感じです:

[1, 2, 4, 6, 8, 10, 12, 14, 16, 18]

これは、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;
}

試してみよう!

今、バイナリサーチの仕組みがわかったので、自分で試してみませんか? 上のコードを使って、数字のリストから8を見つける例を見てみましょう。

let numbers = [1, 2, 4, 6, 8, 10, 12, 14, 16, 18];
let target = 8; let index = binarySearch(numbers, target); console.log(index); // 4

8の番号は4番目です。つまり、リストの5番目の数字なんです!

他の例

// 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' }

まとめ

バイナリサーチは、アイテムのリスト内で何かをすばやく見つけるのに最適な方法です。それは速くて効率的で、多くの異なるタイプの問題を解決するのに使用できます。バイナリサーチの仕組みを知った今、ぜひ試してみませんか?

最後

いつもお世話になっています。この記事を楽しんで、新しいことを学べたら嬉しいです。

今度の記事でお会いしましょう!この記事が気に入ったら、私を応援するために「LIKE」を押して登録してください。ありがとうございました。


この記事の主な目的は、日本語レベルを上げるのを手伝うことです。ソフトウェア開発に関連する概念や知識なとを紹介するために簡単な日本語を使います。ITの知識に関しては、インターネット上でもっとよく説明されているかもしれませんが、この記事の主な目標はまだ日本語を学ぶことです。

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 496

- 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 413

- 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 134

- 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 228