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 Array which is to be searched.
- index : The starting index of the range from which searching will start.
- length : The length of the range in which the search will happen.
- value : The object to search for.
- comparer : When comparing elements then use the IComparer implementation.
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.
Below programs illustrate the use of the above-discussed method:
Example 1: In this example the array stores some string value and find some string value after sorting the array.
using System;
class GFG {
public static void Main()
{
string [] arr = new string [5] { "ABCD" ,
"IJKL" , "XYZ" , "EFGH" , "MNOP" };
Console.WriteLine( "The original Array" );
display(arr);
Console.WriteLine( "\nsorted array" );
Array.Sort(arr);
display(arr);
Console.WriteLine( "\n1st call" );
object obj1 = "EFGH" ;
FindObj(arr, obj1);
Console.WriteLine( "\n2nd call" );
object obj2 = "ABCD" ;
FindObj(arr, obj2);
}
public static void FindObj( string [] Arr,
object Obj)
{
int index = Array.BinarySearch(Arr, 0, 3,
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);
}
}
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: In this example, here we use “CreateInstance()” method to create a typed array and stores some integer value and search some values after sort the array.
using System;
class GFG
{
public static void Main()
{
Array arr = Array.CreateInstance( typeof (Int32), 8);
arr.SetValue(20, 0);
arr.SetValue(10, 1);
arr.SetValue(30, 2);
arr.SetValue(40, 3);
arr.SetValue(50, 4);
arr.SetValue(80, 5);
arr.SetValue(70, 6);
arr.SetValue(60, 7);
Console.WriteLine( "The original Array" );
display(arr);
Console.WriteLine( "\nsorted array" );
Array.Sort(arr);
display(arr);
Console.WriteLine( "\n1st call" );
object obj1 = 10;
FindObj(arr, obj1);
Console.WriteLine( "\n2nd call" );
object obj2 = 60;
FindObj(arr, obj2);
}
public static void FindObj(Array Arr,
object Obj)
{
int index = Array.BinarySearch(Arr, 1, 4,
Obj, StringComparer.CurrentCulture);
if (index < 0)
{
Console.WriteLine( "The object {0} is not found\n" +
"Next larger object is at index {1}" ,
Obj, ~index );
}
else
{
Console.WriteLine( "The object {0} is at " +
"index {1}" , Obj, index );
}
}
public static void display(Array arr)
{
foreach ( int g in arr)
{
Console.WriteLine(g);
}
}
}
|
Output:
The original Array
20
10
30
40
50
80
70
60
sorted array
10
20
30
40
50
60
70
80
1st call
The object 10 is not found
Next larger object is at index 1
2nd call
The object 60 is not found
Next larger object is at index 5
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 :
24 Jan, 2019
Like Article
Save Article