I have a List<>
of one-dimensional segments, each segment is represented by a struct
containing start and end (x1
, x2
). Segments can overlap. The list is sorted by starts (x1
).
How can I efficiently find the shortest segment containing a particular point x
?
Currently, I use list.BinarySearch
to find the first segment for which x
>= x1
, which is efficient. Then I move on though the list one segment at a time, until x
< x1
, to find the shortest segment for which x1
<= x
< x2
, which is not so efficient.
How can this algorithm be possibly improved for better speed? I feel it can be, but I'm stuck.
This question is not a duplicate of Shortest distance between a point and a line segment.