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
using System;
class GFG {
public static void Main(String[] args) {
int [] arr = new int [7] {1,5,7,4,6,2,3};
Array.Sort(arr);
Console.Write("The elements of Sorted Array: ");
display(arr);
object s = 8;
result(arr, s);
object s1 = 4;
result(arr, s1);
}
static void result( int [] arr2, object k) {
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);
}
}
static void display( int [] arr1)
{
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
using System;
class GFG {
public static void Main(String[] args) {
int [] arr = new int [7] {1,5,7,4,6,2,3};
Array.Sort(arr);
Console.Write("The elements of Sorted Array: ");
display(arr);
Console.WriteLine("\nIndex of 9 is : "+
Array.BinarySearch(arr,8));
}
static void display( int [] arr1)
{
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:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
14 Apr, 2022
Like Article
Save Article