I want to build a lookup table using a floating point value as a key. When I query a table with a given floating point key, I want it to return a value whose key is closest to the key query.
However, I do not know in advance if the floating point keys are evenly distributed or not.
For example, my table could be:
key value
1.0 "red"
1.25 "blue"
2.0 "green"
If I request 1.5, I want to return "blue".
Is there a way to build a table so that it has O (n) memory and O (1) lookup? (That is, a hash table). Obviously, there is an O (log (n)) algorithm if I kept key / value pairs sorted, but I'm curious if this binding can be improved.
source
share