C# | Array.BinarySearch(Array, Object, IComparer) Method (original) (raw)
Last Updated : 31 Jan, 2019
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 is to search for.
- comparer : When comparing elements then the IComparer implementation is used.
Return Value: It returns the index of the specified value in the specified array if the value is found otherwise it returns a negative number. There are different cases of return values as follows:
- If the value is not found and value is less than one or more elements in the array, the negative number returned is the bitwise complement of the index of the first element that is larger than value.
- If the value is not found and value is greater than all elements in the array, the negative number returned is the bitwise complement of (the index of the last element plus 1).
- If this method is called with a non-sorted array, the return value can be incorrect and a negative number could be returned, even if the value is present in the array.
Exceptions:
- ArgumentNullException: If the array is null.
- RankException: If array is multidimensional.
- ArgumentException: If the range is less than lower bound OR length is less than 0.
- ArgumentException: If the comparer is null, and value is of a type that is not compatible with the elements of array.
- InvalidOperationException: If the comparer is null, value does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface.
Example 1: In this example, the array stores some string value and find some string value after sorting the array.
csharp `
// C# program to demonstrate the // Array.BinarySearch(Array, // Object, IComparer) Method using System;
class GFG {
// Main Method public static void Main() { // initializes a new Array string[] arr = new string[5] { "ABCD", "IJKL", "XYZ", "EFGH", "MNOP"};
Console.WriteLine("The original Array");
// calling "display" function
display(arr);
Console.WriteLine("\nsorted array");
// sorting the Array
Array.Sort(arr);
display(arr);
Console.WriteLine("\n1st call");
// search for object "EFGH"
object obj1 = "EFGH";
// call the "FindObj" function
FindObj(arr, obj1);
Console.WriteLine("\n2nd call");
object obj2 = "ABCD";
FindObj(arr, obj2);
}
// find object method public static void FindObj(string[] Arr, object Obj) { int index = Array.BinarySearch(Arr, Obj, StringComparer.CurrentCulture);
if (index < 0)
{
Console.WriteLine("The object {0} is not "+
"found\nNext larger object is at"+
" index {1}", Obj, ~index);
}
else
{
Console.WriteLine("The object {0} is at "+
"index {1}", Obj, index);
}
}
// display method public static void display(string[] arr) { foreach(string g in arr) { Console.WriteLine(g); } } }
`
Output:
The original Array ABCD IJKL XYZ EFGH MNOP
sorted array ABCD EFGH IJKL MNOP XYZ
1st call The object EFGH is at index 1
2nd call The object ABCD is at index 0
Example 2:
csharp `
// C# program to demonstrate the // Array.BinarySearch(Array, // Object, IComparer) Method using System;
class GFG {
// Main Method public static void Main() {
// initializes a new Array.
Array arr = Array.CreateInstance(typeof(Int32), 5);
// Array elements
arr.SetValue(20, 0);
arr.SetValue(10, 1);
arr.SetValue(30, 2);
arr.SetValue(40, 3);
arr.SetValue(50, 4);
Console.WriteLine("The original Array");
// calling "display" function
display(arr);
Console.WriteLine("\nsorted array");
// sorting the Array
Array.Sort(arr);
display(arr);
Console.WriteLine("\n1st call");
// search for object 10
object obj1 = 10;
// call the "FindObj" function
FindObj(arr, obj1);
Console.WriteLine("\n2nd call");
object obj2 = 60;
FindObj(arr, obj2);
}
// find object method public static void FindObj(Array Arr, object Obj) { int index = Array.BinarySearch(Arr, Obj, StringComparer.CurrentCulture);
if (index < 0)
{
Console.WriteLine("The object {0} is not found\nNext"+
" larger object is at index {1}", Obj, ~index);
}
else {
Console.WriteLine("The object {0} is at index {1}",
Obj, index);
}
}
// display method public static void display(Array arr) { foreach(int g in arr) { Console.WriteLine(g); } } }
`
Output:
The original Array 20 10 30 40 50
sorted array 10 20 30 40 50
1st call The object 10 is at index 0
2nd call The object 60 is not found Next larger object is at index 5
Reference:
Similar Reads
- 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
- 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
- Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C# Array.BinarySearch(Array, int32, int32, Object) Method is used to search a range of elements in a one-dimensional sorted array for a value, using the IComparable interface implemented by each element of the array and by the specified value. It searches only in a specified boundary that the user defi 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
- C# | List.BinarySearch(T, Comparer ) 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 comparer); Parameters : Here, the parameters are : "item" - Which is the item to locate an 3 min read
- List BinarySearch() Method in C# List.BinarySearch(T) Method uses a binary search algorithm to locate a specific element in the sorted List or a portion of it. There are 3 methods in the overload list of this method as follows:BinarySearch(T)BinarySearch(T, IComparer)BinarySearch(Int32, Int32, T, ICompare 9 min read
- C# | Copying BitArray elements to an Array The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.CopyTo(Array, Int32) method is used to copy the ent 3 min read
- Array.LastIndexOf Method in C# | Set - 1 Array.LastIndexOf method is used to find the last matching element in a one-dimensional array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the overload list of this method as follows: LastIndexO 8 min read
- Array.LastIndexOf Method in C# | Set – 2 Array.LastIndexOf method is used to find the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the over 4 min read
- C# | Check if an array object is equal to another array object An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. Equals(Object) method which is inherited by Array class from object class is used to check whether an array is equal to another array or not. Syntax: public virtua 2 min read