简体   繁体   English

在C#中的二维数组中查找数字对

[英]Find pair of numbers in 2 dimensional array in C#

what is the fast way to find pair of numbers in 2 dimention array 在2维数组中查找数字对的快速方法是什么

i have this array 我有这个数组

 int[,] numbers = new int[,] { { 5, 2 }, { 5, 1 }, { 5, 0 } , ........... };

i have to integers like x,y 我必须像x,y这样的整数

i want to return the index of this pair apper in the array 我想返回数组中这个配对器的索引

if the pair dont exist retun false 如果该对不存在,则重新设置为false

thanks 谢谢

If the array is ordered you can use a binary search. 如果数组是有序的,则可以使用二进制搜索。 Otherwise you will have to do a linear search. 否则,您将必须进行线性搜索。

If the array is not ordered, the best you can do is a linear search - either row by row, column by column, or even by diagonal. 如果不对数组进行排序,则最好的方法是线性搜索-逐行,逐列甚至对角线搜索。

If either of the dimensions of the array are ordered, then you could use binary search. 如果数组的任何一个维是有序的,则可以使用二进制搜索。

If the data is ordered and also conforms to some kind of distribution, you could use a distribution correlation lookup, which could be slightly more efficient than binary search. 如果数据是有序的并且也符合某种分布,则可以使用分布相关性查找,该查找可能比二进制搜索更有效。

对2D数组进行排序,然后使用二进制搜索。

"Fastest" is vague; “最快”是含糊的; fastest to write, or fastest to execute? 最快写,还是最快执行?

The fastest you could get this on an unordered list would be linear, and the actual best and worst cases would depend on the input data. 您在无序列表上获得此最快的速度将是线性的,实际的最佳和最差情况将取决于输入数据。 The fastest overall on an unordered list would probably be: 在无序列表上最快的总体可能是:

var i = 0;
foreach(var subarray in numbers)
{
   if(subarray[0] == x && subarray[1] == y || subarray[0] == y && subarray[1] == x)
     return i;
   i++;
}

Best case is that the first element has the elements in order; 最好的情况是第一个元素具有顺序的元素; 1 element, 2 equality comparisons. 1个元素,2个相等比较。 Worst case is it's not there; 最坏的情况是它不存在。 n elements, n*4 equality comparisons. n个元素,n * 4个相等比较。

You can "fail fast" in situations where it is unlikely that a match will be found, by checking to see if the subarray has at least one element with at least one of the coordinates. 通过检查子数组是否具有至少一个具有至少一个坐标的元素,可以在不太可能找到匹配项的情况下“快速失败”。 If not, don't bother checking for an exact match. 如果没有,请不要打扰检查是否完全匹配。 That makes the worst case that the element is the last one, out of order (n*2 elements, n*6 comparisons), but the best case is that it isn't there (n elements, n*2 comparisons, which if this case is likely is better than the previous). 这使得最坏的情况是该元素是最后一个不按顺序排列的元素(n * 2个元素,n * 6个比较),但最好的情况是它不存在(n个元素,n * 2个比较,如果这种情况可能比以前更好)。

Lastly, the "fail fast" algorithm allows for using Linq to narrow the number of elements on which you make the full set of conditional checks; 最后,“快速失败”算法允许使用Linq来缩小您要进行全套条件检查的元素的数量。 you first look for elements that have at least one of the coordinates (requiring a max of two checks), then check only those for elements that match exactly (a max of four checks). 您首先要查找至少具有一个坐标的元素(最多需要两个检查),然后仅检查那些元素是否完全匹配(最多四个检查)。 Then, you scan the array for the first element you found, which is a relatively fast referential check. 然后,您在数组中扫描找到的第一个元素,这是相对快速的参照检查。

var result = numbers.Where(a=>a[0] == x || a[0] == y)
   .Where(a=>a[0] == x && a[1] == y || a[0] == y && a[1] == x)
   .FirstOrDefault();

if(result != null) return Array.IndexOf(numbers, result);

Best case, it's not there (n elements, n*2 comparisons). 最好的情况是,它不存在(n个元素,n * 2个比较)。 Only slightly worse is the probably-likely case that only one element has either of the coordinates (n+1 elements, n*2+(2 or 4) comparisons). 可能只有一种元素只有两个坐标之一(n + 1个元素,n * 2 +(2或4)个比较)的情况可能会稍微差一点。 Worst case, the match is the last element, out of order, AND every element in the list has a first coordinate that is x or y (n*3 elements, n*7 comparisons, but this is EXTREMELY unlikely in most real data). 最坏的情况是,匹配项是最后一个乱序的元素,并且列表中的每个元素的第一个坐标均为x或y(n * 3个元素,n * 7个比较,但这在大多数实际数据中不太可能) 。 The average case will depend on the number of elements that have at least one of the coordinates as its first value. 平均情况将取决于具有至少一个坐标作为其第一值的元素的数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM