是否有一个排序列表℃的下限功能; K,V>?下限、有一个、功能、列表

2023-09-02 21:08:33 作者:山河不入梦

有没有上下限功能的排序列表< K,V> ?该函数应返回的第一个元素等于或大于指定的键。是否有支持该其它的类?

专家 - 请再次阅读问题。 我并不需要一个函数,返回键,如果它是present。我感兴趣的场景时,有没有确切的关键匹配。

我感兴趣的O(log n)的时间。这意味着,我没有与foreach循环的问题,而是希望有这样做的一个有效的途径。

我对这个做了一些测试。

LINQ的语句没有既不是编译器和运行的机器进行了优化,所以他们通过所有收集要素走路很慢为O(n)。基于迈赫达德Afshari的答案,这里是一个在O(log n)的上的按键集合作品二进制搜索:

 公共静态INT FindFirstIndexGreaterThanOrEqualTo< T>(
            这个IList的< T> sortedCollection,T键
        )其中T:IComparable的< T> {
    INT开始= 0;
    INT端= sortedCollection.Count;
    而(完>开始){
        INT指数=(开始+端)/ 2;
        牛逼EL = sortedCollection [指数]
        如果(el.CompareTo(键)GT; = 0)
            结束=指数;
        其他
            开始=指数+ 1;
    }
    返回结束;
}
 

解决方案

二进制搜索 SortedList.Keys 集合。

在这里,我们走了。这是为O(log的 N 的):

 私有静态诠释二分查找< T>(IList的< T>清单,T值)
{
    如果(表== NULL)
        抛出新ArgumentNullException(名单);
    无功补偿=的Comparer< T> .DEFAULT;
    INT LO = 0,HI = list.Length  -  1;
    而(LO<喜){
            INT M =(HI + LO)/ 2; //这可能溢出;小心。
            如果(comp.Compare(名单[M],值)小于0)LO = M + 1;
            其他喜= M  -  1;
    }
    如果(comp.Compare(名单[LO],值)小于0)LO ++;
    返回LO;
}

公共静态INT FindFirstIndexGreaterThanOrEqualTo< T,U>
                          (本排序列表< T,U>排序列表,T键)
{
    返回二分查找(sortedList.Keys,键);
}
 

如何用Excel制作进销存表

Is there a Lower Bound function on a SortedList<K ,V>? The function should return the first element equal to or greater than the specified key. Is there some other class that supports this?

Guys - please read the question once again. I do not need a function that returns the key if it is present. I'm interested in scenario when there is no exact key matching.

I'm interested in O(log n) time. It means that I do not have a problem with foreach loop, but rather would like to have an efficient way of doing this.

I have done some tests on this.

Linq statements are not optimized by neither the compiler nor runtime machine, so they walk through all collection elements and are slow O(n). Based on Mehrdad Afshari answer, here is a Binary Search that works in O(log n) on the Keys collection:

public static int FindFirstIndexGreaterThanOrEqualTo<T>(
            this IList<T> sortedCollection, T key
        ) where T : IComparable<T> {
    int begin = 0;
    int end = sortedCollection.Count;
    while (end > begin) {
        int index = (begin + end) / 2;
        T el = sortedCollection[index];
        if (el.CompareTo(key) >= 0)
            end = index;
        else
            begin = index + 1;
    }
    return end;
}

解决方案

Binary search the SortedList.Keys collection.

Here we go. This is O(log n):

private static int BinarySearch<T>(IList<T> list, T value)
{
    if (list == null)
        throw new ArgumentNullException("list");
    var comp = Comparer<T>.Default;
    int lo = 0, hi = list.Length - 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<T,U>
                          (this SortedList<T,U> sortedList, T key)
{
    return BinarySearch(sortedList.Keys, key);
}