//C# program to demonstrate the
// Array.BinarySearch(Array,
// Int32, Int32, Object,
// IComparer) Method
using System;
class GFG
{
// Main Method
public static void Main()
{
// initializes a new Array.
Array arr = Array.CreateInstance(typeof(Int32), 8);
// Array elements
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");
// 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, 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 );
}
}
// display method
public static void display(Array arr)
{
foreach (int g in arr)
{
Console.WriteLine(g);
}
}
}