List
- BinarySearch(T)
- BinarySearch(T,IComparer
) - BinarySearch(Int32,Int32,T,IComparer
)
BinarySearch(T)方法
此方法使用默认比较器在整个排序的List
句法:
public int BinarySearch (T item);
在这里,产品将被定位和项目的值可以为空或引用类型的对象。
返回类型:如果找到该项目,则此方法返回要搜索的元素的从零开始的索引,如果找不到,则将返回一个负数,该负数是下一个元素的索引的按位补码,并且补码大于该项目。如果没有更大的元素,则将返回Count的按位补码。
异常:如果默认比较器Default找不到类型T的IComparable
下面的程序说明了上面讨论的方法的使用:
范例1:
// C# program to illustrate the
// List.BinarySearch(T) Method
using System;
using System.Collections.Generic;
class GFG {
// Main Method
public static void Main()
{
// List creation
List Geek = new List();
// List elements
Geek.Add("ABCD");
Geek.Add("QRST");
Geek.Add("XYZ");
Geek.Add("IJKL");
Console.WriteLine("The Original List is:");
foreach(string g in Geek)
{
// prints original List
Console.WriteLine(g);
}
Console.WriteLine("\nThe List in Sorted form");
// sort the List
Geek.Sort();
Console.WriteLine();
foreach(string g in Geek)
{
// prints the sorted List
Console.WriteLine(g);
}
Console.WriteLine("\nInsert EFGH :");
// insert "EFGH" in the List
//"EFGH" insert into its original
// position when the List is sorted
int index = Geek.BinarySearch("EFGH");
if (index < 0)
{
Geek.Insert(~index, "EFGH");
}
Console.WriteLine();
foreach(string g in Geek)
{
// prints the sorted list
// after inserting "EFGH"
Console.WriteLine(g);
}
}
}
The Original List is:
ABCD
QRST
XYZ
IJKL
The List in Sorted form
ABCD
IJKL
QRST
XYZ
Insert EFGH :
ABCD
EFGH
IJKL
QRST
XYZ
示例2:在此示例中,使用一些整数值创建了List,并使用用户定义函数通过BinarySearch(T)方法在List中插入新的整数。
// C# program to illustrate the
// List.BinarySearch(T) Method
using System;
using System.Collections.Generic;
class GFG {
// method for inserting "3"
public void binarySearch(List Geek)
{
// insert "3" in the List
Console.WriteLine("\nInsert 3 :");
// "3" insert into its original
// position when the List is
// sorted
int index = Geek.BinarySearch(3);
if (index < 0)
{
Geek.Insert(~index, 3);
}
foreach(int g in Geek)
{
// prints the sorted list
// after inserting "3"
Console.WriteLine(g);
}
}
}
// Driver Class
public class search {
public static void Main()
{
// List creation
GFG gg = new GFG();
List Geek = new List() {
5, 6, 1, 9};
Console.WriteLine("Original List");
foreach(int g in Geek)
{
Console.WriteLine(g);
// prints original List
}
Console.WriteLine("\nList in Sorted form");
Geek.Sort();
foreach(int g in Geek)
{
Console.WriteLine(g);
// prints the sorted List
}
// calling the method "binarySearch"
gg.binarySearch(Geek);
}
}
Original List
5
6
1
9
List in Sorted form
1
5
6
9
Insert 3 :
1
3
5
6
9
BinarySearch(T)方法
此方法使用指定的比较器在整个排序列表中搜索一个元素,并返回搜索到的元素的从零开始的索引。
句法:
public int BinarySearch (T item, System.Collections.Generic.IComparer
参数:
- item :要查找的项目,对于引用类型,该项目的值可以为null 。
- 比较器:比较元素时使用的IComparer
实现。
返回值:如果找到该项,则此方法返回要搜索的元素的从零开始的索引,如果找不到,则返回一个负数,该负数是大于该元素的下一个元素的索引的按位补码或者,如果没有更大的元素,则为Count的按位补码。
异常:如果比较器为null,并且默认比较器Default找不到类型T的IComparable
下面的程序说明了上述方法的用法:
范例1:
// C# program to demonstrate the
// List.BinarySearch(T,
// IComparer) Method
using System;
using System.Collections.Generic;
class GFG : IComparer {
public int Compare(string x, string y)
{
if (x == null || y == null)
{
return 0;
}
return x.CompareTo(y);
//"CompareTo()" method
}
}
// Driver Class
class geek {
// Main Method
public static void Main()
{
// list creation
List list1 = new List();
// list elements
list1.Add("B");
list1.Add("C");
list1.Add("E");
list1.Add("A");
// prints Original list
Console.WriteLine("Original string");
foreach(string g in list1)
{
Console.WriteLine(g);
}
GFG gg = new GFG();
// sort the list
list1.Sort(gg);
// prits the sorted form of original list
Console.WriteLine("\nList in sorted form");
foreach(string g in list1)
{
Console.WriteLine(g);
}
//"D" is going to insert
//"gg" is the IComparer
int index = list1.BinarySearch("D", gg);
if (index < 0)
{
list1.Insert(~index, "D");
}
// prints the final List
Console.WriteLine("\nAfetr inserting \"D\" in the List");
foreach(string g in list1)
{
Console.WriteLine(g);
}
}
}
Original string
B
C
E
A
List in sorted form
A
B
C
E
Afetr inserting "D" in the List
A
B
C
D
E
示例2:在此示例中,将使用一些整数值创建List,并使用用户定义函数使用BinarySearch(T,Comparer
// C# program to demonstrate the
// List.BinarySearch(T,
// IComparer) Method
using System;
using System.Collections.Generic;
class GFG : IComparer {
public int Compare(int x, int y)
{
if (x == 0 || y == 0)
{
return 0;
}
return x.CompareTo(y);
}
}
// Driver Class
class geek {
// Main Method
public static void Main()
{
// list creation
List list1 = new List() {
5, 6, 1, 9};
// prints Original list
Console.WriteLine("Original string");
foreach(int g in list1)
{
Console.WriteLine(g);
}
// creating object of class GFG
GFG gg = new GFG();
// sort the list
list1.Sort(gg);
// prits the sorted form
// of original list
Console.WriteLine("\nList in sorted form");
foreach(int g in list1)
{
Console.WriteLine(g);
}
bSearch b = new bSearch();
b.binarySearch(list1);
}
}
class bSearch {
public void binarySearch(List list1)
{
// creating object of class GFG
GFG gg = new GFG();
// "3" is going to insert
// "gg" is the IComparer
int index = list1.BinarySearch(3, gg);
if (index < 0)
{
list1.Insert(~index, 3);
}
// prints the final List
Console.WriteLine("\nAfter inserting \"3\" in the List");
foreach(int g in list1)
{
Console.WriteLine(g);
}
}
}
Original string
5
6
1
9
List in sorted form
1
5
6
9
After inserting "3" in the List
1
3
5
6
9
BinarySearch(Int32,Int32,T,IComparer )
此方法用于使用指定的比较器在排序后的List
句法:
public int BinarySearch (int index, int count, T item, System.Collections.Generic.IComparer
参数:
index: It is the zero-based starting index of the range to search.
count: It is the length of the range to search.
item: It is the object to locate. The value can be null for the reference type.
comparer: It is the IComparer
返回值:如果找到该项目,则返回排序后的List
例外情况:
- ArgumentOutOfRangeException :如果索引小于0或count小于0。
- ArgumentException :如果索引和计数不代表有效范围。
- InvalidOperationException :如果比较器为null。
例子:
// C# program to demonstrate the
// List.BinarySearch(Int32,
// Int32, T, Comparer ) method
using System;
using System.Collections.Generic;
class GFG : IComparer
{
public int Compare(int x, int y)
{
if (x == 0 || y == 0)
{
return 0;
}
return x.CompareTo(y);
}
}
class search {
// "binarySearch" function
public void binarySearch(List list1,
int i)
{
Console.WriteLine("\nBinarySearch a "+
"range and Insert 3");
// "gg" is the object of class GFG
GFG gg = new GFG();
// binary search
int index = list1.BinarySearch(0, i,
3, gg);
if (index < 0)
{
// insert "3"
list1.Insert(~index, 3);
i++;
}
Display(list1);
}
// "Display" function
public void Display(List list)
{
foreach( int g in list )
{
Console.WriteLine(g);
}
}
}
// Driver Class
class geek
{
// Main Method
public static void Main()
{
List list1 = new List()
{
// list elements
15,4,2,9,5,7,6,8,10
};
int i = 7;
Console.WriteLine("Original List");
// "d" is the object of
// the class "search"
search d = new search();
// prints Original list
d.Display(list1);
// "gg" is the object
// of class GFG
GFG gg = new GFG();
Console.WriteLine("\nSort a range with "+
"the alternate comparer");
// sort is happens between
// index 1 to 7
list1.Sort(1, i, gg);
// prints sorted list
d.Display(list1);
// call "binarySearch" function
d.binarySearch(list1,i);
}
}
Original List
15
4
2
9
5
7
6
8
10
Sort a range with the alternate comparer
15
2
4
5
6
7
8
9
10
BinarySearch a range and Insert 3
15
2
3
4
5
6
7
8
9
10
笔记:
- 如果List
包含多个具有相同值的元素,则该方法仅返回一个事件,并且它可能返回任何一个事件,而不一定返回第一个。 - List
必须已经根据比较器实现进行了排序;否则,结果不正确。 - 此方法是O(log n)操作,其中n是范围内的元素数。
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1.binarysearch?view=netframework-4.7.2