The fastest general-purpose algorithm is binary search. Depending on the size of the display array, you may consider manual search coding; this may be plausible for size 32, but I would not become much larger. On a microcontroller, a fully advanced binary search can be 50% faster if you're lucky.
But if the mapping is not too non-linear, there is a good alternative.
Divide the range a into k ranges of equal size, where k not much larger than the number of entries in the matching array, so that the mappings of each endpoint of the range either coincide, or one more than the endpoint of the next range. (If possible, what I meant by "not too nonlinear"). Create another array that maps each endpoint to an index into the original array. (You only need indexes, not endpoints, because endpoints are evenly distributed.) For each range, the value of the lower end value corresponds to the index of the smallest value a in the original array not lower than the upper limit of the range. Please note that due to the requirement presented above, each range may be more than one value of a , so that the index of each endpoint will always indicate a value of a for the end of the range, and a value of a value for the beginning of the range will always be either the same, Lieb previous index.
Now, to find the value, first output the corresponding range index, which is a simple linear calculation ( (val - minval)/k ), and then compare the value with the specified value a , looking at the index for comparison. If the value is less than the distorted a , then subtract it from the index. Then return the value of b from the index.
For an example of such an algorithm, see my answer here .
source share