Live data from Hacker News

How fast does interpolation search converge?

lemire.me

11–20 of 27 posts

Re: How fast does interpolation search converge?

#12

As a side note, if you have the possibility to sort data before searching values in it then implement trie: https://en.wikipedia.org/wiki/Trie This is the best for data with very few updates/modifications and a lot of queries. One example would be points of interest on a map, like restaurants on google maps. Not like those restaurants gets updated/modified every single day but you do have clients that while using you…

Tries are insanely good for implementing fuzzy search as well (say based on a Damerau–Levenshtein distance).

Using this approach for a typo-tolerant instant search engine that I am working on: https://github.com/typesense/typesense

Re: How fast does interpolation search converge?

#13
In college I played with a variation of this where you fix the size of the collection to a power of 2 (sprinkling in zeroes/repeated values if needed): you make your initial guess for where to look is just by taking the top bits of the searched-for value, then estimate the number of slots off you were from the top bits of the _difference_ between the value you found and the one you want (clamping your next guess to the first/last item if you'd fall off the end otherwise). In the concrete example I was playing with, it worked to just stop there then do linear search, but you could be cleverer about how long to iterate and when/how to bail out to something with a better worst case.

As Lemire says the problem (a good "problem"!) is that hashtables and B-trees can be quite fast, robust against different distributions, and you have them right at hand. It'd almost have to be some weird situation like you're handed data in a format that kinda fits the requirements and you're looking for how to work with it in-place.

Re: How fast does interpolation search converge?

#14
post #9

Earlier quoted context omitted.

That's actually quite clever. I like it. I can't figure out the puzzle's answer. Hopefully they'll post the solution someday.

Yup that was the answer :-) interleaving the two lets you get the best of both worlds at the cost of a constant-factor penalty. This is a pretty clever & general-purpose meta-algorithm that works for pretty much any problem where you have competing algorithms with different strengths. Quitting and starting over as GGP mentioned is another option that also works for this problem, though it might not work as well for o…

A strategy in use since the 1970s is to quit interpolation search when it starts picking values too close to the edge of the range, switching to binary search afterwards. That's a well known technique for finding the roots of polynomials.

>This being said, I am not aware of interpolation search being actually used productively in software today. If you have a reference to such an artefact, please share!

This is also my response to that note in the article, interpolation search has been well-known in the numerical methods world since ancient times. ;)

Re: How fast does interpolation search converge?

#17
post #5

Earlier quoted context omitted.

Is the trick quit and start doing a binary search?

Ooh, or what about alternating interpolation and binary search steps? Interpolation steps make quick progress if your distribution is correct.

This sounds like Brent’s method for root finding! Also very similar is his method for Unitarians minimization. I ported this into Clojure recently from Python / FORTRAN... not quite literate programming but well documented if you want to give it a peek. https://github.com/littleredcomputer/sicmutils/blob/master/s...

Re: How fast does interpolation search converge?

#18
One good thing about binary search is memory access pattern.

The element in the middle of the array is tested every single time. Similarly, the elements at 25% and 75% of the array are tested very often, in 50% searches/each.

Modern computers have very slow memory compared to computation speed, and multi-level caches to compensate. Binary search RAM access pattern, when done many times, is friendly towards these multi-level caches. The elements at positions like 25%, 50%, 75% will stay in L1D cache.

Re: How fast does interpolation search converge?

#19

As a side note, if you have the possibility to sort data before searching values in it then implement trie: https://en.wikipedia.org/wiki/Trie This is the best for data with very few updates/modifications and a lot of queries. One example would be points of interest on a map, like restaurants on google maps. Not like those restaurants gets updated/modified every single day but you do have clients that while using you…

If you're already using Postgres, isn't that precisely what GiST indexes are meant for?
Post reply on HN