Uniform binary search

Uniform binary search

Uniform binary search is an optimization of the classic binary search algorithm invented by Donald Knuth and given in Knuth's "The Art of Computer Programming". It uses a lookup table to update a single array index, rather than taking the midpoint of an upper and a lower bound on each iteration; therefore, it is optimized for architectures (such as Knuth's MIX) on which

*a table lookup is generally faster than an addition and a shift, and
*many searches will be performed on the same array, or on several arrays of the same length

C implementation

The uniform binary search algorithm looks like this, when implemented in C.

#define LOG_N 42 static int delta [LOG_N] ; void make_delta(int N) { int power = 1; int i = 0; do { int half = power; power <<= 1; delta [i] = (N + half) / power; } while (delta [i++] != 0); } int unisearch(int *a, int key) { int i = delta [0] -1; /* midpoint of array */ int d = 0; while (1) { if (key = a [i] ) return i; else if (delta [d] = 0) return -1; else { if (key < a [i] ) i -= delta [++d] ; else i += delta [++d] ; } } } /* Example of use: */ #define N 10 int main(void) { int i, a [N] = {1,3,5,6,7,9,14,15,17,19}; make_delta(N); for (i=0; i < 20; ++i) printf("%d is at index %d ", i, unisearch(a, i)); return 0; }

References

*Knuth. "The Art of Computer Programming", Volume 3. Page 412, Algorithm C.

External links

* [http://huizen.dto.tudelft.nl/deBruijn/knuth/unizoek.pas An implementation of Knuth's algorithm] in Pascal, by Han de Bruijn


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Search algorithm — In computer science, a search algorithm, broadly speaking, is an algorithm that takes a problem as input and returns a solution to the problem, usually after evaluating a number of possible solutions. Most of the algorithms studied by computer… …   Wikipedia

  • Interpolation search — is an algorithm for searching for a given key value in an indexed array that has been ordered by the values of the key. It parallels how humans search through a telephone book for a particular name, the key value by which the book s entries are… …   Wikipedia

  • Fibonacci search technique — The Fibonacci search technique is a method of searching a sorted array using a divide and conquer algorithm that narrows down possible locations with the aid of Fibonacci numbers.Compared to binary search, Fibonacci search has the property of… …   Wikipedia

  • Police stop, search, detention and arrest powers in the United Kingdom — United Kingdom law provides for the police to stop and search members of the public without making an arrest. Scotland has a separate legal identity to England and Wales and stop and search powers are therefore provided for by different… …   Wikipedia

  • List of terms relating to algorithms and data structures — The [http://www.nist.gov/dads/ NIST Dictionary of Algorithms and Data Structures] is a reference work maintained by the U.S. National Institute of Standards and Technology. It defines a large number of terms relating to algorithms and data… …   Wikipedia

  • Список терминов, относящихся к алгоритмам и структурам данных —   Это служебный список статей, созданный для координации работ по развитию темы.   Данное предупреждение не устанавливается на информационные списки и глоссарии …   Википедия

  • Список терминов — Список терминов, относящихся к алгоритмам и структурам данных   Это сл …   Википедия

  • Hash table — Not to be confused with Hash list or Hash tree. Unordered map redirects here. For the proposed C++ class, see unordered map (C++). Hash Table Type unsorted dictionary Invented 1953 Time complexity in big O notation Average Worst case Space …   Wikipedia

  • Splay tree — A splay tree is a self balancing binary search tree with the additional property that recently accessed elements are quick to access again. It performs basic operations such as insertion, look up and removal in O(log(n)) amortized time. For many… …   Wikipedia

  • Hash function — A hash function is any well defined procedure or mathematical function for turning some kind of data into a relatively small integer, that may serve as an index into an array. The values returned by a hash function are called hash values, hash… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”