Open In App

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 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:



Exceptions:

Below programs illustrate the above-discussed method: Example 1: 




// 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: 




// 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:

Reference:


Article Tags :
C#