HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includi ...
, an exponential search (also called doubling search or galloping search or Struzik search) is an
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
, created by Jon Bentley and
Andrew Chi-Chih Yao Andrew Chi-Chih Yao (; born December 24, 1946) is a Chinese computer scientist and computational theorist. He is currently a professor and the dean of Institute for Interdisciplinary Information Sciences (IIIS) at Tsinghua University. Yao us ...
in 1976, for searching sorted, unbounded/infinite lists. There are numerous ways to implement this with the most common being to determine a range that the search key resides in and performing a
binary search In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the ...
within that range. This takes ''O''(log ''i'') where ''i'' is the position of the search key in the list, if the search key is in the list, or the position where the search key should be, if the search key is not in the list. Exponential search can also be used to search in bounded lists. Exponential search can even out-perform more traditional searches for bounded lists, such as binary search, when the element being searched for is near the beginning of the array. This is because exponential search will run in ''O''(log ''i'') time, where ''i'' is the index of the element being searched for in the list, whereas binary search would run in ''O''(log ''n'') time, where ''n'' is the number of elements in the list.


Algorithm

Exponential search allows for searching through a sorted, unbounded list for a specified input value (the search "key"). The algorithm consists of two stages. The first stage determines a range in which the search key would reside if it were in the list. In the second stage, a binary search is performed on this range. In the first stage, assuming that the list is sorted in ascending order, the algorithm looks for the first exponent, ''j'', where the value 2 is greater than the search key. This value, 2 becomes the upper bound for the binary search with the previous power of 2, 2, being the lower bound for the binary search. // Returns the position of key in the array arr of length size. template int exponential_search(T arr[], int size, T key) In each step, the algorithm compares the search key value with the key value at the current search index. If the element at the current index is smaller than the search key, the algorithm repeats, skipping to the next search index by doubling it, calculating the next power of 2. If the element at the current index is larger than the search key, the algorithm now knows that the search key, if it is contained in the list at all, is located in the interval formed by the previous search index, 2, and the current search index, 2. The binary search is then performed with the result of either a failure, if the search key is not in the list, or the position of the search key in the list.


Performance

The first stage of the algorithm takes ''O''(log ''i'') time, where ''i'' is the index where the search key would be in the list. This is because, in determining the upper bound for the binary search, the while loop is executed exactly \lceil \log(i) \rceil times. Since the list is sorted, after doubling the search index \lceil \log(i) \rceil times, the algorithm will be at a search index that is greater than or equal to ''i'' as 2^ \ge i. As such, the first stage of the algorithm takes ''O''(log ''i'') time. The second part of the algorithm also takes ''O''(log ''i'') time. As the second stage is simply a binary search, it takes ''O''(log ''n'') where ''n'' is the size of the interval being searched. The size of this interval would be 2 - 2 where, as seen above, ''j'' = log ''i''. This means that the size of the interval being searched is 2 - 2 = 2. This gives us a run time of log (2) = log (''i'') - 1 = ''O''(log ''i''). This gives the algorithm a total runtime, calculated by summing the runtimes of the two stages, of ''O''(log ''i'') + ''O''(log ''i'') = 2 ''O''(log ''i'') = ''O''(log ''i'').


Alternatives

Bentley and Yao suggested several variations for exponential search. These variations consist of performing a binary search, as opposed to a unary search, when determining the upper bound for the binary search in the second stage of the algorithm. This splits the first stage of the algorithm into two parts, making the algorithm a three-stage algorithm overall. The new first stage determines a value ''j''', much like before, such that 2^ is larger than the search key and 2^ is lower than the search key. Previously, ''j''' was determined in a unary fashion by calculating the next power of 2 (i.e., adding 1 to ''j''). In the variation, it is proposed that j' is doubled instead (e.g., jumping from 2 to 2 as opposed to 2). The first ''j''' such that 2^ is greater than the search key forms a much rougher upper bound than before. Once this ''j''' is found, the algorithm moves to its second stage and a binary search is performed on the interval formed by j'/2 and j', giving the more accurate upper bound exponent ''j''. From here, the third stage of the algorithm performs the binary search on the interval 2 and 2, as before. The performance of this variation is \lfloor \log i \rfloor + 2 \lfloor \log(\lfloor \log i \rfloor + 1)\rfloor + 1 = ''O''(log ''i''). Bentley and Yao generalize this variation into one where any number, ''k'', of binary searches are performed during the first stage of the algorithm, giving the ''k''-nested binary search variation. The asymptotic runtime does not change for the variations, running in ''O''(log ''i'') time, as with the original exponential search algorithm. Also, a data structure with a tight version of the dynamic finger property can be given when the above result of the ''k''-nested binary search is used on a sorted array. Using this, the number of comparisons done during a search is log (''d'') + log log (''d'') + ... + ''O''(log ''d''), where ''d'' is the difference in rank between the last element that was accessed and the current element being accessed.


See also

* Linear search *
Binary search In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the ...
*
Interpolation search Interpolation search is an algorithm for Search algorithm, searching for a key in an array that has been Collation, ordered by numerical values assigned to the keys (''key values''). It was first described by W. W. Peterson in 1957. Interpolation ...
*
Ternary search A ternary search algorithm is a technique in computer science for finding the minimum or maximum of a unimodal function. A ternary search determines either that the minimum or maximum cannot be in the first third of the domain or that it cannot be ...
*
Hash table In computing, a hash table, also known as hash map, is a data structure that implements an associative array or dictionary. It is an abstract data type that maps keys to values. A hash table uses a hash function to compute an ''index'', ...


References

{{Reflist, refs= {{citation, contribution=Fast intersection algorithms for sorted sequences, first1=Ricardo, last1=Baeza-Yates, author1-link= Ricardo Baeza-Yates , first2=Alejandro, last2=Salinger, title=Algorithms and Applications: Essays Dedicated to Esko Ukkonen on the Occasion of His 60th Birthday, volume=6060, series=Lecture Notes in Computer Science, editor1-first=Tapio, editor1-last=Elomaa, editor2-first=Heikki, editor2-last=Mannila, editor2-link=Heikki Mannila, editor3-first=Pekka, editor3-last=Orponen, publisher=Springer, year=2010, isbn=9783642124754, pages=45–61, doi=10.1007/978-3-642-12476-1_3, bibcode=2010LNCS.6060...45B. {{cite journal , last1 = Bentley , first1 = Jon L. , author1-link = Jon Bentley (computer scientist) , last2 = Yao , first2 = Andrew C. , author2-link = Andrew Yao , title = An almost optimal algorithm for unbounded searching , year = 1976 , journal = Information Processing Letters , volume = 5 , issue = 3 , pages = 82–87 , issn = 0020-0190 , doi = 10.1016/0020-0190(76)90071-5 {{cite web , url = https://sites.google.com/site/d7013e11/announcements/exponentialbinarysearch , accessdate = 2014-03-24 , title = Exponential Binary Search , first = Håkan , last = Jonsson , date = 2011-04-19 {{cite journal , last1 = Andersson , first1 = Arne , last2 = Thorup , first2 = Mikkel , author2-link = Mikkel Thorup , title = Dynamic ordered sets with exponential search trees , year = 2007 , journal =
Journal of the ACM The ''Journal of the ACM'' is a peer-reviewed scientific journal covering computer science in general, especially theoretical aspects. It is an official journal of the Association for Computing Machinery. Its current editor-in-chief is Venkatesan ...
, volume = 54 , issue = 3 , pages = 13 , issn = 0004-5411 , doi = 10.1145/1236457.1236460 , arxiv= cs/0210006
Search algorithms