Generic SortedList, How to find the index of the first element that is greater than the search key?

不想你离开。 提交于 2020-01-24 05:46:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!