如何执行上的IList℃的二进制搜索; T>? [英] How to perform a binary search on IList<T>?

查看:271
本文介绍了如何执行上的IList℃的二进制搜索; T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题 - 给定一个的IList< T> 你怎么不自己写的方法,并没有将数据复制到一个类型与内置的二进制执行二进制搜索搜索支持。我现在的状态如下。

Simple question - given an IList<T> how do you perform a binary search without writing the method yourself and without copying the data to a type with build-in binary search support. My current status is the following.

  • 名单,其中,T&GT; .BinarySearch()不是的IList℃的成员; T&GT;
  • 没有等效的<一个href="http://msdn.microsoft.com/en-us/library/system.collections.arraylist.adapter.aspx"><$c$c>ArrayList.Adapter()方法名单,其中,T&GT;
  • 的IList&LT; T&GT; 不从的IList 继承,因此使用 ArrayList.Adapter ()不可能
  • List<T>.BinarySearch() is not a member of IList<T>
  • There is no equivalent of the ArrayList.Adapter() method for List<T>
  • IList<T> does not inherit from IList, hence using ArrayList.Adapter() is not possible

我倾向于认为这是不可能的,内置的方式,但我无法相信,这样一个基本的方法是从BCL / FCL丢失。

I tend to believe that is not possible with build-in methods, but I cannot believe that such a basic method is missing from the BCL/FCL.

如果这是不可能的,谁可以给最短,最快,最聪明的,或最美丽的二进制搜索实现的IList&LT; T&GT;

If it is not possible, who can give the shortest, fastest, smartest, or most beatiful binary search implementation for IList<T>?

更新

我们都知道,一个列表必须使用二进制搜索,因此你可以认为它是之前进行排序。但我认为(但没有证实),这是同样的问题,那种 - 你怎么样的IList&LT; T&GT;

We all know that a list must be sorted before using binary search, hence you can assume that it is. But I assume (but did not verify) it is the same problem with sort - how do you sort IList<T>?

结论

目前似乎没有内置的二进制搜索的IList&LT; T&GT; 。我们可以使用一()排序依据() LINQ的方法来搜索和排序,但它会likly有性能击中。实现它自己(作为一个扩展的方法)似乎是最好的,你可以做。

There seems to be no build-in binary search for IList<T>. One can use First() and OrderBy() LINQ methods to search and sort, but it will likly have a performance hit. Implementing it yourself (as an extension method) seems the best you can do.

推荐答案

我怀疑有一个通用的二进制搜索方法,.NET这样的,除了一个是present在一些基础类(但显然不是在接口),所以这里是我的通用之一。

I doubt there is a general purpose binary search method in .NET like that, except for the one being present in some base classes (but apparently not in the interfaces), so here's my general purpose one.

public static Int32 BinarySearchIndexOf<T>(this IList<T> list, T value, IComparer<T> comparer = null)
{
    if (list == null)
        throw new ArgumentNullException("list");

    comparer = comparer ?? Comparer<T>.Default;

    Int32 lower = 0;
    Int32 upper = list.Count - 1;

    while (lower <= upper)
    {
        Int32 middle = lower + (upper - lower) / 2;
        Int32 comparisonResult = comparer.Compare(value, list[middle]);
        if (comparisonResult == 0)
            return middle;
        else if (comparisonResult < 0)
            upper = middle - 1;
        else
            lower = middle + 1;
    }

    return -1;
}

这当然假定列表中的问题已经排序,根据该比较器将使用相同​​的规则。

This of course assumes that the list in question is already sorted, according to the same rules that the comparer will use.

这篇关于如何执行上的IList℃的二进制搜索; T&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆