📌  相关文章
📜  Array.BinarySearch(Array,Object)方法,带有C#中的示例

📅  最后修改于: 2021-05-29 21:53:29             🧑  作者: Mango

此方法用于通过使用IComparable接口在整个一维排序数组中搜索特定元素,该接口由数组的每个元素和指定的对象实现。

句法:

public static int BinarySearch (Array array, object value);

参数:

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

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

例外情况:

  • ArgumentNullException:如果数组null
  • RankException:如果数组是多维的。
  • ArgumentException:如果该的类型与array的元素不兼容。
  • InValidOperationException:如果该未实现IComparable接口,并且搜索遇到一个未实现IComparable接口的元素。

下面的程序说明了上面讨论的方法:

范例1:

// C# program to illustrate the 
// Array.BinarySearch(Array, Object)
// Method
using System;
  
class GFG {
      
    // Main Method
    public static void Main(String[] args) {
          
      
    // taking an 1-D Array
    int[] arr = new int[7] {1,5,7,4,6,2,3};
      
    // for this method array
    // must be sorted
    Array.Sort(arr);
      
    Console.Write("The elements of Sorted Array: ");
      
    // calling the method to
    // print the values
    display(arr);
      
    // taking the element which is 
    // to search for in a variable
    // It is not present in the array
    object s = 8;
      
      
    // calling the method containing
    // BinarySearch method
    result(arr, s);
      
    // taking the element which is 
    // to search for in a variable
    // It is present in the array
    object s1 = 4;
      
      
    // calling the method containing
    // BinarySearch method
    result(arr, s1);
      
}
  
// containg BinarySearch Method
static void result(int[] arr2, object k) {
          
    // using the method
    int res = Array.BinarySearch(arr2, k);
      
    if (res < 0)
    {
        Console.WriteLine("\nThe element to search for "+
                            "({0}) is not found.", k);
    }
      
    else
    {
        Console.WriteLine("The element to search for "+
                    "({0}) is at index {1}.", k, res);
    }
          
}
      
  
// display method
static void display(int[] arr1) 
{
      
    // Displaying Elements of array 
    foreach(int i in arr1) 
        Console.Write(i + " "); 
      
}
      
}
输出:
The elements of Sorted Array: 1 2 3 4 5 6 7 
The element to search for (8) is not found.
The element to search for (4) is at index 3.

范例2:

// C# program to illustrate the 
// Array.BinarySearch(Array, Object)
// Method
using System;
  
class GFG {
      
    // Main Method
    public static void Main(String[] args) {
          
      
    // taking an 1-D Array
    int[] arr = new int[7] {1,5,7,4,6,2,3};
      
    // for this method array
    // must be sorted
    Array.Sort(arr);
      
    Console.Write("The elements of Sorted Array: ");
      
    // calling the method to
    // print the values
    display(arr);
      
    // it will return a negative value as 
    // 9 is not present in the array
    Console.WriteLine("\nIndex of 9 is: "+ 
               Array.BinarySearch(arr,8));
      
}
      
  
// display method
static void display(int[] arr1) 
{
      
    // Displaying Elements of array 
    foreach(int i in arr1) 
        Console.Write(i + " "); 
      
}
      
}
输出:
The elements of Sorted Array: 1 2 3 4 5 6 7 
Index of 9 is: -8

重要事项:

  • 对于此方法,数组必须按排序顺序(即升序)进行。
  • 此方法不支持搜索包含负索引的数组。
  • 允许重复的元素。如果Array包含多个等于该值的元素,则该方法仅返回其中一个事件的索引,而不一定返回第一个事件的索引。
  • 总是可以将null与任何其他引用类型进行比较;因此,与null的比较不会产生异常。
  • 此方法是O(log n)操作,其中n是数组的长度。

参考:

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