C# | List.BinarySearch(T, Comparer<T> )
Last Updated :
22 Jan, 2019
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, IComparer<T> comparer);
Parameters :
Here, the parameters are :
"item" - Which is the item to locate and the value of
item can be
null or
reference type.
"comparer" - When comparing elements then use the
IComparer implementation.
Returns :
If 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.
Exceptions :
InvalidOperationException
Example :
[sourcecode language="csharp"]
//C# program to demonstrate List.BinarySearch(T, Comparer <T>) method
using System;
using System.Collections.Generic;
class GFG : IComparer<string>
{
public int Compare(string x, string y)
{
if (x == null || y == null)
{
return 0;
}
return x.CompareTo(y);
//"CompareTo()" method
}
}
class geek
{
public static void Main()
{
//list creation
List<string> list1 = new List<string>();
//list elements
list1.Add("B");
list1.Add("C");
list1.Add("E");
list1.Add("A");
//prints Original list
Console.WriteLine("Original string");
foreach(string g in list1)
{
Console.WriteLine(g);
}
GFG gg = new GFG();
list1.Sort(gg);
//sort the list
//prits the sorted form of original list
Console.WriteLine("\nList in sorted form");
foreach (string g in list1)
{
Console.WriteLine(g);
}
int index = list1.BinarySearch("D", gg);
//"D" is going to insert
//"gg" is the IComparer
if (index < 0)
{
list1.Insert(~index, "D");
}
//prints the final List
Console.WriteLine("\nAfetr inserting \"D\" in the List");
foreach (string g in list1)
{
Console.WriteLine(g);
}
}
}
[/sourcecode]
Output :
Original string
B
C
E
A
List in sorted form
A
B
C
E
Afetr 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 define function.
[sourcecode language="csharp"]
//C# program to demonstrate List.BinarySearch(T, Comparer <T>) method
using System;
using System.Collections.Generic;
class GFG : IComparer<int>
{
public int Compare(int x, int y)
{
if (x == 0 || y == 0)
{
return 0;
}
return x.CompareTo(y);
}
}
class geek
{
public static void Main()
{
//list creation
List<int> list1 = new List<int>()
{
5,6,1,9
//list elements
};
//prints Original list
Console.WriteLine("Original string");
foreach(int g in list1)
{
Console.WriteLine(g);
}
GFG gg = new GFG();
//creating object of class GFG
list1.Sort(gg);
//sort the list
//prits the sorted form of original list
Console.WriteLine("\nList in sorted form");
foreach (int g in list1)
{
Console.WriteLine(g);
}
bSearch b = new bSearch();
b.binarySearch(list1);
}
}
class bSearch
{
public void binarySearch(List<int> list1)
{
GFG gg = new GFG();
//creating object of class GFG
int index = list1.BinarySearch(3, gg);
//"3" is going to insert
//"gg" is the IComparer
if (index < 0)
{
list1.Insert(~index, 3);
}
//prints the final List
Console.WriteLine("\nAfter inserting \"3\" in the List");
foreach (int g in list1)
{
Console.WriteLine(g);
}
}
}
[/sourcecode]
Output :
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
Similar Reads
List BinarySearch() Method in C# 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:BinarySearch(T)BinarySearch(T, IComparer<T>)BinarySearch(Int32, Int32, T, ICompare
9 min read
C# | Array.BinarySearch(Array, Int32, Int32, Object, IComparer) Method This method searches for an element which is in a one-dimensional sorted array within a range of elements using a specified IComparer interface. Syntax: public static int BinarySearch(Array arr, int index, int length, Object value, IComparer comparer) Parameters: arr : The sorted one-dimensional Arr
4 min read
C# | Array.BinarySearch(Array, Object, IComparer) Method This method searches for a value in a one-dimensional sorted array using a specified IComparer interface. Syntax: public static int BinarySearch(Array arr, Object val, IComparer comparer) Parameters: arr : The one-dimensional sorted array in which the search will happen. val : The object value which
4 min read
How to use Array.BinarySearch() Method in C# | Set -2 Array.BinarySearch() method is used to search a value in a sorted one dimensional array. The binary search algorithm is used by this method. This algorithm searches a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the
12 min read
Array.BinarySearch(Array, Object) Method with examples in C# This method is used to search a specific element in the entire one-dimensional sorted array by using the IComparable interface which is implemented by each element of the array and by the specified object. Syntax: public static int BinarySearch (Array array, object value); Parameters: array: It is t
4 min read