Faster than binary search for ordered list

后端 未结 11 1737
自闭症患者
自闭症患者 2020-12-12 13:51

is there an algorithm that is faster than binary search, for searching in sorted values of array?

in my case, I have a sorted values (could be any type values) in an

11条回答
  •  粉色の甜心
    2020-12-12 14:03

    It's been mentioned in misc comments, but I would think a natural & simple answer to this particular question ("any-type, values in an array") would be an Interpolation Search:

    Instead of calculating the midpoint, interpolation search estimates the position of the target value, taking into account the lowest and highest elements in the array as well as length of the array. It works on the basis that the midpoint is not the best guess in many cases. For example, if the target value is close to the highest element in the array, it is likely to be located near the end of the array.

    Quote from: https://en.wikipedia.org/wiki/Binary_search_algorithm

    Main page: https://en.wikipedia.org/wiki/Interpolation_search

    Under the assumption of a uniform distribution it can approach O(log log N)

    Since CPUs are so fast compared to memory access these days (program for RAM like you once did for disk) the index / comparison calculations are likely cheap compared to each data fetch. It might also be possible to eke out a little more performance with a linear search once the search is sufficiently narrowed (exploiting memory / cache locality).

提交回复
热议问题