请输入您要查询的字词:

 

单词 BinarySearch
释义

binary search


The Problem

Let be a total orderingPlanetmathPlanetmath on the set S. Given a sequence of n elements,L={x1x2xn}, and a value yS,locate the position of any elements in L that are equal to y, or determine thatnone exist.

The Algorithm

The binary searchMathworldPlanetmath technique is a fundamental method for locating an element ofa particular value within a sequence of sorted elements (see Sorting Problem).The idea is to eliminate half of the search space with each comparison.

First, themiddle element of the sequence is compared to the value we are searching for. If thiselement matches the value we are searching for, we are done. If, however, the middleelement is “less than” the value we are chosen for (as specified by the relationMathworldPlanetmathPlanetmath (http://planetmath.org/Relation) usedto specify a total order over the set of elements), then we know that, if the value existsin the sequence, it must exist somewhere after the middle element. Therefore wecan eliminate the first half of the sequence from our search and simply repeat the searchin the exact same manner on the remaining half of the sequence. If, however, the value weare searching for comes before the middle element, then we repeat the search on the first halfof the sequence.

Pseudocode

AlgorithmMathworldPlanetmath Binary_Search(L, n, key)
Input: A list L of n elements, and key (the search key)
Output: Position (such that X[Position]=key)

begin
         PositionFind(L,1,n,key);
end
function
Find(L,bottom,top,key)
begin
         if bottom=top then

if L[bottom]=key then

Findbottom

else

Find0

else

begin

middle(bottom+top)/2;

if key<L[middle] then

FindFind(L,bottom,middle-1,key)

else

FindFind(L,middle+1,top,key)

end
end

Analysis

We can specify the runtime complexity of this binary search algorithm by counting the numberof comparisons required to locate some element y in L. Since half of the list is eliminatedwith each comparison, there can be no more than log2n comparisons before either the positionsof all the y elements are found or the entire list is eliminated and y is determined to not existin L.Thus the worst-case runtime complexity of the binary search is 𝒪(logn).It can also be shown that the average-case runtime complexity of the binary search is approximatelylog2n-1 comparisons.This means that any single entry in a phone book containing one million entries can be locatedwith at most 20 comparisons (and on average 19).

Titlebinary search
Canonical nameBinarySearch
Date of creation2013-03-22 11:44:34
Last modified on2013-03-22 11:44:34
Ownermathcam (2727)
Last modified bymathcam (2727)
Numerical id18
Authormathcam (2727)
Entry typeAlgorithm
Classificationmsc 68P10
Classificationmsc 16S40
Classificationmsc 20G42
Classificationmsc 16W35
Classificationmsc 81R50
Classificationmsc 16W30
Classificationmsc 57T05
Classificationmsc 54-01
Related topicTotalOrder
Related topicPartialOrder
Related topicSortingProblem
Related topicInsertionSort
随便看

 

数学辞典收录了18232条数学词条,基本涵盖了常用数学知识及数学英语单词词组的翻译及用法,是数学学习的有利工具。

 

Copyright © 2000-2023 Newdu.com.com All Rights Reserved
更新时间:2025/5/4 20:47:04