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:

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:

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