Array.BinarySearch(Array, Object) Method with examples in C# (original) (raw)
Last Updated : 14 Apr, 2022
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 the sorted 1-D array to search. value: It is the object to search for.
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 the array is multidimensional.
- ArgumentException: If the value is of a type which is not compatible with the elements of the array.
- InValidOperationException: If the value does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface.
Below programs illustrate the above-discussed method: Example 1:
csharp `
// C# program to illustrate the // Array.BinarySearch(Array, Object) // Method using System;
class GFG {
// Main Method
public static void Main(String[] args) {
// taking an 1-D Array
int[] arr = new int[7] {1,5,7,4,6,2,3};
// for this method array
// must be sorted
Array.Sort(arr);
Console.Write("The elements of Sorted Array: ");
// calling the method to
// print the values
display(arr);
// taking the element which is
// to search for in a variable
// It is not present in the array
object s = 8;
// calling the method containing
// BinarySearch method
result(arr, s);
// taking the element which is
// to search for in a variable
// It is present in the array
object s1 = 4;
// calling the method containing
// BinarySearch method
result(arr, s1);
}
// containing BinarySearch Method static void result(int[] arr2, object k) {
// using the method
int res = Array.BinarySearch(arr2, k);
if (res < 0)
{
Console.WriteLine("\nThe element to search for "+
"({0}) is not found.", k);
}
else
{
Console.WriteLine("The element to search for "+
"({0}) is at index {1}.", k, res);
}
}
// display method static void display(int[] arr1) {
// Displaying Elements of array
foreach(int i in arr1)
Console.Write(i + " ");
}
}
`
Output:
The elements of Sorted Array: 1 2 3 4 5 6 7 The element to search for (8) is not found. The element to search for (4) is at index 3.
Example 2:
csharp `
// C# program to illustrate the // Array.BinarySearch(Array, Object) // Method using System;
class GFG {
// Main Method
public static void Main(String[] args) {
// taking an 1-D Array
int[] arr = new int[7] {1,5,7,4,6,2,3};
// for this method array
// must be sorted
Array.Sort(arr);
Console.Write("The elements of Sorted Array: ");
// calling the method to
// print the values
display(arr);
// it will return a negative value as
// 9 is not present in the array
Console.WriteLine("\nIndex of 9 is: "+
Array.BinarySearch(arr,8));
}
// display method static void display(int[] arr1) {
// Displaying Elements of array
foreach(int i in arr1)
Console.Write(i + " ");
}
}
`
Output:
The elements of Sorted Array: 1 2 3 4 5 6 7 Index of 9 is: -8
Important Points:
- For this method, the array must in sorted order i.e ascending order.
- This method does not support searching arrays that contain negative indexes.
- Duplicate elements are allowed. If the Array contains more than one element equal to the value, the method returns the index of only one of the occurrences, and not necessarily the first one.
- null can always be compared with any other reference type; therefore, comparisons with null do not generate an exception.
- This method is an O(log n) operation, where n is the Length of the array.
Reference:
Similar Reads
- 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
- 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
- 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
- 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.GetValue() Method in C# with Examples | Set – 4 Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32)Array.GetValue(Int64, Int64)Array.GetValue(Int32)Array.GetValue(Int64)Array.GetValue( 5 min read
- Array.GetValue() Method in C# with Examples | Set – 2 Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32) Array.GetValue(Int64, Int64) Array.GetValue(Int32) Array.GetValue(Int64) Array.GetVa 3 min read
- Array.GetValue() Method in C# with Examples | Set - 1 Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32) Array.GetValue(Int64, Int64) Array.GetValue(Int32) Array.GetValue(Int64) Array.GetVa 4 min read
- Array.GetValue() Method in C# with Examples | Set – 3 Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32)Array.GetValue(Int64, Int64)Array.GetValue(Int32)Array.GetValue(Int64)Array.GetValue( 5 min read
- SortedList ContainsKey() Method in C# With Examples Given a SortedList object, now our task is to check whether the given SortedList object contains the specific key or not. So to do this task we use ContainsKey() method. This method is used to determine whether a SortedList object contains a specific key or not. It will return true if the key is fou 2 min read
- BitArray.RightShift() Method in C# with Examples BitArray class manages a array of bit values, which are represented as Booleans, where true indicates bit is 1 and false indicates bit is 0. This class is contained in namespace, System.Collections. BitArray.RightShift(Int32) method is used to shift the bits of the bit array to the right by one posi 2 min read