Open In App

C# | List.BinarySearch(T, Comparer<T> )

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
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


Next Article
Article Tags :

Similar Reads