Generic SortedList, How do I find the index of the first item that is larger than the search key?

Without using extension methods (LINQ). Unfortunately, I am limited to .NET 2.0. (Yes, that sucks)

Looking for something close to O (log (n)).

Thank you for your help.

+3
source share
3 answers

To find the first key that is larger than the given one, you can use the list of keys SortedList<T>.Keysand perform a binary search or Interpolation search on the keys. This will give O(log(n))( MSDN claims to be a key search O(1)).

+5
source

Binary search to search for O (n log n).

+2
source

, O (log n)

, O (n) . , .

The idea of ​​making the values ​​in order in order takes O (n * log (n)), if you don't do it again and again and cache the result, why bother? just use linear search.

(note that you are interested in finding a value, not a key)

0
source

Source: https://habr.com/ru/post/1710019/


All Articles