DQ JA%oPyqoPv{v{v{v{v{v{v{v{v{v{{Kiy'{Ky'{Ky'aAaECAC-~L-| 666666666999VCXBCBWZ

BinarySearch` optimizes searches on sorted collections. We evaluate the BinarySearch method on List and arrays. We may have a variable number of elements. Binary search is an amazing algorithm that hones in on values in sorted collections. `Complexity: `Binary search has O(log n) complexity, making it more efficient than others.`510px`360px`Example.` Here we look at an example program that uses the BinarySearch instance method on the List type. You must pass this method a value of the type used in the List. Normally, programs use strings, so we use that type here. `Three values are looked up.` The locations of "peach", "banana", and "apple" are looked up in the List. Note that the List is in alphabetical order. BinarySearch won't work if your List or array is not already sorted. `It doesn't matter if you use numeric, alphanumeric, or ASCII sorting—BinarySearch will work with any of these.`Discussion.` I found here that binary search becomes more efficient in comparison to a linear search with large collections. For small collections of less than about 100 elements, BinarySearch is slower, probably due to implementation overhead.`Wikipedia states` that binary search "makes progressively better guesses, and closes in on the location of the sought value by selecting the middle element in the span." See "Binary Search Algorithm" on Wikipedia. `Binary search algorithm: Wikipedia `https://en.wikipedia.org/wiki/Binary_search_algorithm`MSDN states that the BinarySearch method on List, which we will use, "returns the zero-based index of the element" we request.`List.BinarySearch Method: MSDN `http://msdn.microsoft.com/en-us/library/w4e7fxsh(VS.80).aspx`Dictionary` has a constant lookup time. It is faster than List with any search algorithm on the data I used. You should choose Dictionary for most scenarios. As the number of elements grows, BinarySearch is faster than a linear search. `With huge collections, with millions of elements, the cost of building up a Dictionary could eclipse the time saved on lookup.`Summary.` We used the BinarySearch instance method on the List type in the C# language. BinarySearch uses a better algorithm than iterative searches for large collections, but normally falls short of Dictionary. `Array.BinarySearch `array-binarysearch

DEF EE; E EE { DED{ DDE<E> lEEE<E>(); DDl.E"acorn");FD E0 DDFl.E"apple");FD E1 DDFl.E"banana");FD E2 DDFl.E"cantaloupe");F E3 DDFl.E"lettuce");FDE4 DDFl.E"onion");FD E5 DDFl.E"peach");FD E6 DDFl.E"pepper");FD E7 DDFl.E"squash");FD E8 DDFl.E"tangerine");F E9 DDEThis Es the index of "peach". DDFEiEl.FBinarySearchF("peach"); DDEi);F DDEThis Es the index of "banana". DDFiEl.FBinarySearchF("banana"); DDEi);F DDEThis Es the index of "apple". DDFiEl.FBinarySearchF("apple"); DDEi); D} } F 6 2 1F

!\BinarySearch