Binary search algorithm

The binary search is an algorithm ( ie ' in a list " ) very efficiently takes place on a field or a sought element provides a reliable statement about the absence of this element. The prerequisite is that the elements arranged in the field according to a total order relation ( " sorted " ) are. The algorithm is based on a simple form of divide and conquer scheme, at the same time it also provides a greedy algorithm dar. order and subsequent search must refer to the same key.

  • 3.1 of intervals
  • 3.2 Binary search tree
  • 3.3 Binary Search and total quasi-ordering
  • 3.4 Interpolation
  • 3.5 Quadratic binary search
  • 4.1 C
  • Python 4.2
  • 4.3 Haskell

Algorithm

First, the central element of the array is checked. It may be smaller, larger or equal to the desired element. If it is smaller than the element sought, the desired element in the back half must be inserted, if it is there at all. Is it, however, greater, further research is needed only in the front half. The other half does not have to be considered. Is it equal to the desired element, the search is finished.

In the to be examined half ( and again in the following halves ) procedure is the same: The middle element provides again the decision about whether and where further research is needed. The length of the search area is bisected by step to step. At least when the search area is shrunk to a single element, the search is finished. This one element is either the desired element or the element searched for is not found.

The algorithm for binary search is implemented as either iteration or recursion. To use it, the data must already be sorted and present in a data structure that can be accessed in the " right " to the nth element. In a simple linked list, the efficiency would be lost (but see skip- list).

Complexity

Order of entries found in a field element or determine that this is not in the field, steps are needed maximum. It is therefore the binary search, expressed in the Landau notation in the complexity class. This makes it significantly faster than the linear search, but which has the advantage to work in unsorted boxes. In special cases, the interpolation search to be faster than the binary search.

Expected value of the complexity

In a search set of elements, each step is accompanied by the division of the search set. The probability of the search argument is to find a set of elements then. If one is reached by successively halving the search set on the last element, you still need another query, so a further step to determine whether this is the search argument. Overall, the probability of the event to find the search argument until the second step:

The complexity as the number of required steps to abort the search in an amount of elements is a random variable and is valid for the expected value:

From this it follows (see also series (mathematics) )

Similar methods and variants

Of intervals

The binary search method described here can be viewed as a (finite ) expression of nested intervals from the mathematical analysis.

Binary search tree

The search algorithm is also looking into a binary search tree, if one interprets the array as such: the central element is the root, the centers of the roots of the corresponding sub-trees and so forth of the resulting halves. The resulting binary tree, this interpretation is even balanced. Does not one in the middle, so the result is still a binary search tree, but it may not be more balanced.

The great superiority of the binary search tree over binary search in the array lies firstly in better behavior with insertions and deletions, which on average are subject to a linear effort. For trees, there is in these cases implementations with guaranteed logarithmic term. There also the memory management is easier because changes do not affect the entire array, but can be connected directly to the emergence or disappearance of an element. Second, trees can be better than the array adapted to frequencies. However, if the array is already sorted and ready then no longer changes and access probabilities should play no role, the array is a good method.

Binary Search and total quasi-ordering

Because the array can be viewed as a finite domain of a function, which need not necessarily be injective, of course, the occurrence of duplicates can easily regulate the function values ​​. And if the order relation from the outset is already no total order, but only a total quasi-ordering, it may be more economical to form the equivalence classes before they are compared as to keep all possible duplicates in the array.

Example

Although in a collection of keywords to uppercase and lower case letters allowed, but the keywords should not differ in their importance.

Interpolation

In the interpolation search the array is not divided in the middle, but estimated the position of the desired element by linear interpolation. Are the key distributed approximately equidistant, the search item can be found in almost constant time. In an unfavorable case, however, the running time is linear. Apart from that the domain must also be suited for linear interpolation.

Quadratic binary search

In the square binary search attempting to those of the normal binary search to combine the advantages of the Interpolation and to reduce by means of interpolation in each iteration the search space at an interval of length.

Various implementations

In many programming languages ​​, this algorithm is available in the class libraries. In Java, for example, as java.util.Arrays.binarySearch and java.util.Collections.binarySearch, bisect in python as the package and in C / STL as std :: binary_search in the " algorithms" header.

The return value is returned, the field position at which the searched entry was found. Could not find the entry, the position is usually returned, where he would stand, but eg negative - as an indication of " not found ". Subsequent implementations give but in this case only "0" or " -1" ( " not found ").

In large fields, the calculation of the center position

Variable: mid = ( Left Right ) DIV 2 in addition lead to an overflow, so often instead

Variable: mid = Left ( ( right - left ) DIV 2) is implemented.

C

Example in C ( iterative):

/ **   * Binary search on the array M after the entry worth seeking   *   * @ Param M: by seeking to field   * @ Param entries: Number of array elements   * @ Param search value: sought entry   * @ Return > = 0: index of the entry   * <0: nothing found   * / int binary_search (const int M [ ], const int entries, int const seeking value) {      const int NICHT_GEFUNDEN = -1;      int mid;      int left = 0; / * We start with the smallest index * /      int right = entries - 1; / * Array index starts at 0 * /        / * As long as the amount to be searched is not empty * /      while ( left < = right)      {          mid = left ( ( right - left ) / 2); / * Halve area * /            if ( M [ middle ] == search value) / * element seeking value found? * /              return middle;          else              if ( M [ mid ] > search value)                  right = middle - 1; / * In the left section continue searching * /              else / * (M [ middle ] < search value) * /                  left = middle 1; / * Keep looking in the right section * /              / * End if * /          / * End if * /      }        return NICHT_GEFUNDEN; } Python

Recursive method in Python:

Def binaersuche_rekursiv ( values, wanted, start, end ):      if end < start:          return ' not found'      mid = ( start end ) / 2      if values ​​[ mid ] == looking for:          return center      elif values ​​[ mid ] < looking for:          binaersuche_rekursiv return ( values, searched, middle 1, end )      else:          binaersuche_rekursiv return ( values, wanted, start, mid -1) Haskell

Example in the functional programming language Haskell (recursively):

Data SearchResult = NotFound Integer | Integer Found Deriving (Eq, Ord, Show)   bsearch :: Ord a = > a - hereafter is searched                   -> (Integer -> a) - to search result, monotone                   - > Integer - index lower limit (inclusive)                   - > Integer - index upper limit ( exclusive)                   -> Search Result bsearch lo hi s least      | S lo> dest = NotFound lo      | Hi < = lo = lo NotFound      | Otherwise = go lo hi      where          - Invariants: lo < hi, lo is always small enough hi is always too big.          - We are looking for the * biggest * index, which is small enough.          go lo hi              | Lo 1 == hi = if s == lo lo else least then Found NotFound hi              | Least

  • Binary search algorithm during the week
  • Search algorithm

Pictures of Binary search algorithm

125657
de