Simple question - given an IList
how do you perform a binary search without writing the method yourself and without copying the data to a type with bui
If you need a ready-made implementation for binary search on IList
s, Wintellect's Power Collections has one (in Algorithms.cs
):
///
/// Searches a sorted list for an item via binary search. The list must be sorted
/// by the natural ordering of the type (it's implementation of IComparable<T>).
///
/// The sorted list to search.
/// The item to search for.
/// Returns the first index at which the item can be found. If the return
/// value is zero, indicating that was not present in the list, then this
/// returns the index at which could be inserted to maintain the sorted
/// order of the list.
/// The number of items equal to that appear in the list.
public static int BinarySearch(IList list, T item, out int index)
where T: IComparable
{
// ...
}