此方法使用指定的IComparer接口在一维排序数组中搜索值。
句法:
public static int BinarySearch(Array arr, Object val, IComparer comparer)
参数:
- arr :将在其中进行搜索的一维排序数组。
- val :要搜索的对象值。
- 比较器:比较元素时,将使用IComparer实现。
返回值:如果找到该值,则返回指定数组中指定值的索引,否则返回负数。返回值有以下几种不同的情况:
- 如果未找到该值并且value小于数组中的一个或多个元素,则返回的负数是大于value的第一个元素的索引的按位补码。
- 如果未找到该值并且该值大于数组中的所有元素,则返回的负数为(最后一个元素的索引加1)的按位补码。
- 如果使用非排序数组调用此方法,则即使数组中存在该值,返回值也可能不正确,并且可能返回负数。
例外情况:
- ArgumentNullException:如果数组为null。
- RankException:如果数组是多维的。
- ArgumentException:如果范围小于下限OR长度小于0。
- ArgumentException:如果比较器为null,并且value的类型与array的元素不兼容。
- InvalidOperationException:如果比较器为null,则value不实现IComparable接口,并且搜索遇到不实现IComparable接口的元素。
示例1:在此示例中,数组存储一些字符串值,并对数组进行排序后找到一些字符串值。
// 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);
}
}
}
输出:
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
范例2:
// 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);
}
}
}
输出:
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
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.array.binarysearch?view=netframework-4.7.2#System_Array_BinarySearch_System_Array_System_Object_System_Collections_IComparer_