Collection Classes
- slide 16 : 36
Sample use of BinarySearch in
List<T>
An illustration of two overloads of
BinarySearch
Sample uses of List.BinarySearch.
Output from the BinarySearch program.
Searching for a non-existing Point.
Output from the BinarySearch program - non-existing Point.
Lessons learned
Binary search can only be done on sorted lists
In order to use binary search, we need - in general - to provide an explicit
Comparer
object
Binary search returns a (non-negative) integer if the element is found
The index of the located element
Binary search returns a negative integer if the element is not found
The complement of this number is a
ghost index
The index of the element if it had been in the list