views:

174

answers:

6

I'm looking at List and I see a BinarySearch method with a few overloads, and I can't help wondering if it makes sense at all to have a method like that in List?

Why would I want to do a binary search unless the list was sorted? And if the list wasn't sorted, calling the method would just be a waste of CPU time. What's the point of having that method on List?

+2  A: 

yes but List has Sort() method as well so you can call it before BinarySearch.

Arseny
+3  A: 

Sorting and searching are two very common operations on lists. It would be unfriendly to limit a developer's options by not offering binary search on a regular list.

Library design requires compromises - the .NET designers chose to offer the binary search function on both arrays an lists in C# because they likely felt (as I do) that these are useful and common operations, and programmers who choose to use them understand their prerequisites (namely that the list is ordered) before calling them.

It's easy enough to sort a List<T> using one of the Sort() overloads. If you feel that you need an invariant that gaurantees sorting, you can always use SortedList<TKey,TValue> or SortedSet<T> instead.

LBushkin
+1  A: 

I agree it's completely dumb to Call BinarySearch on an unsorted list, but it's perfect if you know your large list is sorted.

I've used it when checking if items from a stream exist in a (more or less) static list of 100,000 items or more.

Binary Searching the list is ORDERS of magnitude faster than doing a list.Find, which is many orders of magnitude faster than a database look up.

I makes sense, and I'm glad it there (not that it would be rocket science to implement it if it wasn't).

Binary Worrier
+2  A: 

Others have pointed out that BinarySearch is quite useful on a sorted List<T>. It doesn't really belong on List<T>, though, as anyone with C++ STL experience would immediately recognize.

With recent C# language developments, it makes more sense to define the notion of a sorted list (e.g., ISortedList<T> : IList<T>) and define BinarySearch (et. al.) as extension methods of that interface. This is a cleaner, more orthogonal type of design.

I've started doing just that as part of the Nito.Linq library. I expect the first stable release to be in a couple of months.

Stephen Cleary
I'm not sure what you're getting at when comparing it to the C++ STL. std::list<> is a linked-list, but List<> is actually implemented as an array and is closer to std::vector<>.
James Curran
The point is that the C++ STL supported *orthogonality*. `vector` did not have a method named `binary_search`; rather, `binary_search` could be applied to any random-access iterator, such as those provided by `vector`. Apply the same concept to the C# BCL: `List<T>` should not provide `BinarySearch`; neither should `IList<T>`. It should be an extension method for any `IList<T>` (thus achieving orthogonality of the `BinarySearch` algorithm over any random-access iterator/container). (cont).
Stephen Cleary
In my answer (and library), I go a step further than this and define an `ISortedList<T>`, which is a random-access iterator/container that is known (at compile time) to be sorted. It is natural then to define `BinarySearch` as an extension method on `ISortedList<T>`.
Stephen Cleary
+2  A: 

BinarySearch only makes sense on a List<T> that is sorted, just like IList<T>.Add only makes sense for an IList<T> with IsReadOnly = false. It's messy, but it's just something to deal with: sometimes functionality X depends on criterion Y. The fact that Y isn't always true doesn't make X useless.

Now, in my opinion, it's frustrating that .NET doesn't have general Sort and BinarySearch methods for any IList<T> implementation (e.g., as extension methods). If it did, we could easily sort and search for items within any non-read-only collection providing random access.

Then again, you can always write your own (or copy someone else's).

Dan Tao
+6  A: 

I note in addition to the other correct answers that binary search is surprisingly hard to write correctly. There are lots of corner cases and some tricky integer arithmetic. Since binary search is obviously a common operation on sorted lists, the BCL team did the world a service by writing the binary search algorithm correctly once rather than encouraging customers to all write their own binary search algorithm; a significant number of those customer-authored algorithms would be wrong.

Eric Lippert
Even great library designers get it wrong: http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html
Ron Warholic
@Eric Lippert: Thank you, Eric.
Water Cooler v2