# What is the name for this search algorithm?

A colleague came up with the following search algorithm:

• We have an array of sorted, distinct integers [$i_0$ < $i_2$ < ... < $i_n$]
• We are looking for the index of $i_k$ (for simplicity suppose that $i_k$ is in the array)
• We suppose that the elements are pretty uniformly distributed between $i_1$ and $i_n$

The algorithm:

1. let "position" be initially be 0
2. if $i_k$ == $i_{position}$, stop
3. otherwise:
• calculate distance = $i_k$ - $i_{position}$
• if we have just switched direction (eg. the previous distance was positive and this one is negative or vice-versa), make sure that abs(distance) < abs(previous distance) by adding/subtracting 1 from distance such that it gets closer to 0. Note: if distance is already is 1 or -1, it isn't adjusted.
• let position be position + distance (see note below for possible optimization)
• if position is positive, let position be min(position, n) (ie. don't overshoot the end)
• if position is negative, let position be max(position, 0) (ie. don't undershoot the beginning)

I would be interested if somebody studied this algorithm and if so, what's its name? The best I could come up with is that it's a variant of "interpolation search", however I expect the above algorithm to be faster on x86 hardware because it only uses addition/subtraction which is faster than the division/multiplication used by "standard" interpolation search.

Note:

Instead of updating position by adding the distance (ie. position = position + distance) we can use the "average distance between consecutive elements" as a scaling factor: $$position \leftarrow position + distance / {i_n-i_0 \over n+1}$$

This should give a more precise estimate about the element position.

A disadvantage of the above is that it involves a division which is slow(er) on x86/x64 platforms. Perhaps we can get away with finding the closes power of two to the scaling factor (average distance between consecutive elements) and use shift to perform the division (ie. if $2^t$ is the power of two closes to the scaling factor we can do $position \leftarrow position + distance >> k$.

Update:

• as people have pointed out the initial element needs to be $i_0$
• added steps to avoid oscillating indefinitely between undershooting / overshooting the target
• discovered the "galloping search" from Timsort which seems related but not the same
• This algorithm doesn't seem to work correctly. On the array $\{1,3,5\}$, searching for $3$ it will jump between $1$ and $5$ forever. Feb 24, 2015 at 14:00
• Unless your array indices start at 0, the first time you do step 2 you'll refer to a non-existent array location. Feb 24, 2015 at 14:12
• @RickDecker - correct, I updated the text to reflect the fact that the array starts at 0. Feb 24, 2015 at 20:01
• @TomvanderZanden - correct. I updated the algorithm to avoid this "oscillating indefinitely" situation. Feb 24, 2015 at 20:01
• @Cd-Man That resolves my concern with it oscillating indefinitely. However, it is still a pretty inefficient search algorithm since on an array $\{2,4,\ldots,2k-2,2k,2k+2,\ldots,4k-2,4k\}$, searching for $2k$ it needs linear time since it triggers your "stop it jumping" thing every step. Feb 24, 2015 at 20:49

You may be interested to a related algorithm, known as Interpolation Search, which achieves on average the complexity of $O(\log \log n)$ for searching $n$ keys. This is the usual search you do when you scan a phone book for example.
{"ft_lang_label":"__label__en","ft_lang_prob":0.91381806,"math_prob":0.98480916,"size":2350,"snap":"2023-40-2023-50","text_gpt3_token_len":557,"char_repetition_ratio":0.14109121,"word_repetition_ratio":0.0,"special_character_ratio":0.24553192,"punctuation_ratio":0.07862408,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99628174,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-03T06:45:58Z\",\"WARC-Record-ID\":\"<urn:uuid:6dc291c1-1b61-49a1-8d81-9321284c055f>\",\"Content-Length\":\"166634\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f5074557-309a-4a71-aa20-065d96e9a698>\",\"WARC-Concurrent-To\":\"<urn:uuid:7eb6e322-8af6-4560-84ea-122508f91d07>\",\"WARC-IP-Address\":\"104.18.43.226\",\"WARC-Target-URI\":\"https://cs.stackexchange.com/questions/39744/what-is-the-name-for-this-search-algorithm\",\"WARC-Payload-Digest\":\"sha1:IV7VC2D2FQ4SXM743R6RRBRCOG5FJZYH\",\"WARC-Block-Digest\":\"sha1:U5T76G4UR6ALADP2CDUZSEB344JE3UT2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100489.16_warc_CC-MAIN-20231203062445-20231203092445-00688.warc.gz\"}"}