二进制搜索SortedList.Keys
集合.
开始了.这是O(log n):
private static int BinarySearch(IList list, T value) { if (list == null) throw new ArgumentNullException("list"); var comp = Comparer .Default; int lo = 0, hi = list.Count - 1; while (lo < hi) { int m = (hi + lo) / 2; // this might overflow; be careful. if (comp.Compare(list[m], value) < 0) lo = m + 1; else hi = m - 1; } if (comp.Compare(list[lo], value) < 0) lo++; return lo; } public static int FindFirstIndexGreaterThanOrEqualTo (this SortedList sortedList, T key) { return BinarySearch(sortedList.Keys, key); }