问题
Without using extensions methods (LINQ). I am restricted to .NET 2.0 unfortunately. (Yeah, it sucks)
Looking for something close to O(log(n)).
Thanks for your help.
回答1:
To find the first key that is greater than a given key you could use the list of keys SortedList<T>.Keys and perform a Binary Search or Interpolation Search on the keys. This will yield O(log(n)) (MSDN states that a key look up is O(1)).
回答2:
Binary search it for an O(n log n) lookup.
回答3:
search where things are in oder O(log n) binary search
search for where things are not in order O(n) linear. Can't do better if things are not in order.
The idea of making the values in order by order takes O(n * log(n)) unless you are going to be doing this over and over and caching the result why bother? just use your linear search.
(note thought you were interested in searching the value not the key)
来源:https://stackoverflow.com/questions/966649/generic-sortedlist-how-to-find-the-index-of-the-first-element-that-is-greater-t