个性化阅读
专注于IT技术分析

如何在C#中使用Array.BinarySearch()方法|S1

点击下载

Array.BinarySearch()方法用于搜索排序的值一维数组。的二进制搜索算法用于此方法。该算法通过将搜索间隔重复分成两半来搜索排序的数组。从覆盖整个数组的间隔开始。如果搜索键的值小于间隔中间的项目, 请将间隔缩小到下半部分。否则, 将其缩小到上半部分。重复检查, 直到找到该值或间隔为空。

重要事项:

  • 在调用此方法之前, 必须对数组进行排序。
  • 如果数组不包含指定的值, 则此方法将返回负整数。
  • 数组必须是一维的, 否则无法使用此方法。
  • Icomparable接口必须由值或数组的每个元素实现。
  • 如果在数组中找到多个匹配的元素, 则该方法将仅返回一个事件的索引, 并且不必将索引作为第一个事件。

此方法的重载列表中共有8种方法, 如下所示:

BinarySearch(数组, 对象)

BinarySearch(Array, Object, IComparer)

BinarySearch(Array, Int32, Int32, Object)

BinarySearch(Array, Int32, Int32, Object, IComparer)

BinarySearch <T>(T [], T)

BinarySearch <T>(T [], T, IComparer <T>)

BinarySearch <T>(T [], Int32, Int32, T)

BinarySearch <T>(T [], Int32, Int32, T, IComparer <T>)

BinarySearch(Array, Object)方法

此方法用于搜索整个一维排序的特定元素Array。它使用了由一维数组的每个元素和指定对象实现的IComparable接口。此方法是O(log n)操作, 其中n是指定数组的Length。

语法:public static int BinarySearch(数组arr, 对象val);参数:arr:要搜索的排序一维数组。 val:它是要搜索的对象。

返回值:它返回指定索引值在指定的rr如果值被发现, 否则返回负数。返回值有以下几种不同的情况:

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

异常情况:

  • ArgumentNullException:如果rrisnull.
  • RankException:如果rr是多维的。
  • ArgumentException:如果值的类型与rr.
  • 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

BinarySearch(Array, Object, IComparer)方法

此方法用于搜索整个一维排序的特定元素Array使用指定的IComparer接口。

语法:public static int BinarySearch(Array arr, Object val, IComparer比较器)参数:arr:将在其中进行搜索的一维排序数组。 val:要搜索的对象值。比较器:比较元素时, 将使用IComparer实现。

返回值:它返回指定索引值在指定的rr如果值被发现, 否则返回负数。返回值有以下几种不同的情况:

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

异常情况:

  • ArgumentNullException:如果rr一片空白。
  • RankException:Ifrr是多维的。
  • ArgumentException:如果范围小于下限OR长度小于0。
  • ArgumentException:如果比较器为null, 并且value的类型与的元素不兼容rr.
  • InvalidOperationException:如果比较器为null, 值不实现IComparable接口, 并且搜索遇到不实现IComparable接口的元素。

例子:

//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

BinarySearch(Array, Int32, Int32, Object)方法

此方法用于搜索一维排序数组中元素范围内的值。它使用由数组的每个元素和指定值实现的IComparable接口。它仅在用户定义的指定边界内搜索。

语法:public static int BinarySearch(Array arr, int i, int len, object val);参数:arr:这是一维数组, 用户必须在其中搜索元素。 i:这是用户要开始搜索的范围的起始索引。 len:用户要搜索的范围的长度。 val:是用户要搜索的值。

返回值:它返回指定索引值在指定的rr如果值被发现, 否则返回负数。返回值有以下几种不同的情况:

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

异常情况:

  • ArgumentNullException:如果rr一片空白。
  • RankException:Ifrr是多维的。
  • ArgumentOutOfRangeException:如果索引小于数组的下限OR长度小于0。
  • ArgumentException:如果索引和长度未在数组中指定有效范围OR该值的类型与数组的元素不兼容。
  • InvalidOperationException:如果value未实现IComparable接口, 并且搜索遇到未实现IComparable接口的元素。

例子:

//C# Program to illustrate the use of
//Array.BinarySearch(Array, Int32, //Int32, Object) Method
using System;
using System.IO;
  
class GFG {
  
     //Main Method
     static void Main()
     {
         //initializing the integer array
         int [] intArr = { 42, 5, 7, 12, 56, 1, 32 };
  
         //sorts the intArray as it must be
         //sorted before using method
         Array.Sort(intArr);
  
         //printing the sorted array
         foreach ( int i in intArr) Console.Write(i + " "
                                                + "\n" );
  
         //intArr is the array we want to find
         //and 1 is the starting index
         //of the range to search. 5 is the
         //length of the range to search.
         //32 is the object to search
         int index = Array.BinarySearch(intArr, 1, 5, 32);
  
         if (index>= 0) {
  
             //if the element is found it
             //returns the index of the element
             Console.WriteLine( "Index of 32 is : " + index);
         }
  
         else {
  
             //if the element is not
             //present in the array or
             //if it is not in the
             //specified range it prints this
             Console.Write( "Element is not found" );
         }
  
         //intArr is the array we want to
         //find. and 1 is the starting
         //index of the range to search. 5 is
         //the length of the range to search
         //44 is the object to search
         int index1 = Array.BinarySearch(intArr, 1, 5, 44);
  
         //as the element is not present
         //it prints a negative value.
         Console.WriteLine( "Index of 44 is :" + index1);
     }
}

输出如下:

1 
5 
7 
12 
32 
42 
56 
Index of 32 is : 4
Index of 44 is :-7

BinarySearch(Array, Int32, Int32, Object, IComparer)方法

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

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

返回值:它返回指定索引值在指定的rr, 如果值被发现, 否则返回负数。返回值有以下几种不同的情况:

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

例子:在此示例中, 我们使用”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

赞(0)
未经允许不得转载:srcmini » 如何在C#中使用Array.BinarySearch()方法|S1

评论 抢沙发

评论前必须登录!