📌  相关文章
📜  C#| Array.BinarySearch(Array,Int32,Int32,Object,IComparer)方法

📅  最后修改于: 2021-05-29 20:42:45             🧑  作者: Mango

此方法使用指定的IComparer接口在一系列元素内搜索一维排序数组中的元素。

句法:

参数:

  • arr :要搜索的排序一维数组。
  • index :搜索将开始的范围的起始索引。
  • length :将在其中进行搜索的范围的长度。
  • value :要搜索的对象。
  • 比较器:比较元素时,请使用IComparer实现。

返回值:如果找到该值,则返回指定数组中指定的索引,否则返回负数。返回值有以下几种不同的情况:

  • 如果未找到该并且value小于数组中的一个或多个元素,则返回的负数是大于value的第一个元素的索引的按位补码。
  • 如果未找到该并且该大于数组中的所有元素,则返回的负数为(最后一个元素的索引加1)的按位补码。
  • 如果使用非排序数组调用此方法,则即使数组中存在该,返回值也可能不正确,并且可能返回负数。

下面的程序说明了上述方法的用法:

示例1:在此示例中,数组存储一些字符串值,并对数组进行排序后找到一些字符串值。

// 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
    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, 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);
    }
}
  
// 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:在此示例中,这里我们使用“ CreateInstance() ”方法创建类型化的数组,并存储一些整数值并在对数组进行排序后搜索一些值。

//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);
    }
}
}
输出:
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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.array.binarysearch?view=netframework-4.7.2#System_Array_BinarySearch_System_Array_System_Int32_System_Int32_System_Object_System_Collections_IComparer_