Efficient search of sorted numerical values

后端 未结 4 699
不思量自难忘°
不思量自难忘° 2021-02-06 15:06

I have an int[] array that contains values with the following properties:

  • They are sorted
  • They are unique (
4条回答
  •  旧巷少年郎
    2021-02-06 15:33

    This is in the comments and should be an answer. It's a joint effort, so I'm making it a CW answer:

    You may want to look at an interpolation search. In the worst case, they can be worse than O(log n) and so if that's a hard requirement, this wouldn't apply. But if your interpolation is decent, depending on the data distribution an interpolation search can beat a straight binary.

    To know, you'd have to implement the interpolation search with a reasonably smart interpolation algorithm, and then run several representative data sets through both to see whether the interpolation or the binary is better suited. I'd think it'd be one of the two, but I'm not au fait with truly cutting edge searching algorithms.

提交回复
热议问题