-
C# |数组。BinarySearch(数组,Int32,Int32,对象,IComparer)方法
C# |数组。BinarySearch(数组,Int32,Int32,对象,IComparer)方法
原文:https://www . geeksforgeeks . org/c-sharp-array-binarysearcharray-int 32-int 32-object-I comparer-method/
此方法使用指定的 IComparer 接口在元素范围内搜索一维排序数组中的元素。
语法:
公共静态整数二进制搜索(数组数组、整数索引、整数长度、对象值、整数比较器)
参数:
- arr :待搜索的排序一维数组。
- 索引:搜索开始范围的起始索引。
- 长度:搜索将发生的范围的长度。
- 值:要搜索的对象。
- 比较器:当比较元素时,使用比较器实现。
返回值:如果找到值,则返回指定的数组中指定的值的索引,否则返回负数。返回值有如下几种不同的情况:
- 如果在数组中没有找到值并且值小于一个或多个元素,则返回的负数是大于值的第一个元素的索引的按位补码。
- 如果找不到值并且值大于数组中的所有元素,则返回的负数是(最后一个元素的索引加 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);
}
}
}
Output:
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);
}
}
}
Output:
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/en-us/dotnet/API/system . array . binary search?view = net framework-4 . 7 . 2 # System _ Array _ binary search _ System _ Array _ System _ int 32 _ System _ int 32 _ System _ Object _ System _ Collections _ IComparer _
版权属于:月萌API www.moonapi.com,转载请注明出处
本文链接:https://www.moonapi.com/news/27799.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数