search
HomeCommon Problembinary search algorithm

binary search algorithm

Jun 03, 2019 pm 04:12 PM
binary search

Binary search is also called binary search, which is a more efficient search method. However, binary search requires that the linear table must adopt a sequential storage structure, and the elements in the table must be arranged in order by keywords.

binary search algorithm

Search process

First, assuming that the elements in the table are arranged in ascending order, record the key in the middle of the table The word is compared with the search keyword. If the two are equal, the search is successful; otherwise, the middle position record is used to divide the table into the first and last sub-tables. If the keyword recorded in the middle position is greater than the search keyword, the previous sub-table is further searched. , otherwise search further for the next sub-table. Repeat the above process until a record that meets the conditions is found, making the search successful, or until the subtable does not exist, in which case the search fails.

Number of comparisons

Calculation formula:

When the sequence table has n keywords:

When the search fails, compare the keyword at least a times; when the search succeeds, the maximum number of keyword comparisons is b.

Note: a, b, n are all positive integers.

Algorithm complexity

The basic idea of ​​binary search is to divide n elements into two roughly equal parts, and compare a[n/2] with x, If x=a[n/2], then x is found and the algorithm is terminated; if xa[n/2], Then just search for x in the right half of array a.

The time complexity is nothing more than the number of while loops!

There are n elements in total,

gradually follows n, n/2, n/4,....n/2^k (the remaining number of elements will be operated next ), where k is the number of loops

Since after you round n/2^k>=1

, you can get n/2^k=1

k=log2n, (based on base 2, the logarithm of n)

So the time complexity can be expressed as O(h)=O(log2n)

The following provides a bisection Find the pseudocode of the implementation:

BinarySearch(max,min,des)

mid-

while(min

mid=(min max)/2

if mid=des then

return mid

elseif mid >des then

max=mid-1

else

min=mid 1

return max

The half search method is also called the binary search method. It makes full use of the order relationship between elements and adopts the divide-and-conquer strategy, which can complete the search task in O(log n) in the worst case. Its basic idea is: (assuming that the array elements are arranged in ascending order) divide n elements into two halves with roughly the same number, take a[n/2] and compare it with the x you want to find, if x=a[n/ 2] then x is found and the algorithm terminates; if x

The above is the detailed content of binary search algorithm. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft