![]() |
VOOZH | about |
List<T>.BinarySearch(T) Method uses a binary search algorithm to locate a specific element in the sorted List<T> or a portion of it. There are 3 methods in the overload list of this method as follows:
This method searches for an element in the entire sorted List<T> using the default comparer and returns the zero-based index of the searched element.
Syntax:
public int BinarySearch (T item);
Here, item is the object which is to be locate and the value of item can be null or reference type.
Return Type: If the item is found, then this method returns the zero-based index of the element to be searched for and if not found, then a negative number that is the bitwise complement of the index of the next element will be return and the complement is larger than that item. If there is no larger element, the bitwise complement of Count will be return.
Exception: This method will give InvalidOperationException if the default comparer Default cannot find an implementation of the IComparable<T> generic interface or the IComparable interface for type T.
Below programs illustrate the use of above-discussed method:
Example 1:
The Original List is: ABCD QRST XYZ IJKL The List in Sorted form ABCD IJKL QRST XYZ Insert EFGH : ABCD EFGH IJKL QRST XYZ
Example 2: In this example, the List is created with some integer values and to insert a new integer using BinarySearch(T) method in the List by using a user define function.
Original List 5 6 1 9 List in Sorted form 1 5 6 9 Insert 3 : 1 3 5 6 9
This method searches for an element in the entire sorted List using the specified comparer and returns the zero-based index of the searched element.
Syntax:
public int BinarySearch (T item, System.Collections.Generic.IComparer<T> comparer);
Parameters:
Return Value: If the item founds, then this method returns the zero-based index of the element to be searched for and if not found, then a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of Count.
Exception: This method will give InvalidOperationException if the comparer is null, and the default comparer Default cannot find an implementation of the IComparable<T> generic interface or the IComparable interface for type T.
Below programs illustrate the use of the above-discussed method:
Example 1:
Output:
Original string
B
C
E
A
List in sorted form
A
B
C
E
After inserting "D" in the List
A
B
C
D
E
Example 2: In this example, the List is created with some integer values and to insert a new integer using BinarySearch(T, Comparer <T>) method in the List by using a user defined function.
Original string 5 6 1 9 List in sorted form 1 5 6 9 After inserting "3" in the List 1 3 5 6 9
This method is used to search a range of elements in the sorted List<T> for an element using the specified comparer and returns the zero-based index of the element.
Syntax:
public int BinarySearch (int index, int count, T item, System.Collections.Generic.IComparer<T> comparer);
Parameters:
index: It is the zero-based starting index of the range to search.
count: It is the length of the range to search.
item: It is the object to locate. The value can be null for the reference type.
comparer: It is the IComparer implementation to use when comparing elements, or null to use the default comparer Default.
Return Value: It returns the zero-based index of item in the sorted List<T>, if the item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of Count.
Exceptions:
Example:
Original List 15 4 2 9 5 7 6 8 10 Sort a range with the alternate comparer 15 2 4 5 6 7 8 9 10 BinarySearch a range and Insert 3 15 2 3 4 5 6 7 8 9 10
Note:
Reference: