简体   繁体   English

二进制搜索多维数组中的第一个元素

[英]Binary Search on the first element in a multiple dimensional array

My goal is to perform a binary search for only the first element in a 2D array. 我的目标是仅对二维数组中的第一个元素执行二进制搜索。 I have been searching all day to find if it is possible using BinarySearch() in .NET but I can't find a thing. 我一整天都在寻找是否有可能在.NET中使用BinarySearch()但我找不到东西。

To make this clearer. 为了使这更清楚。 Imagine I had a 1D array, unsorted. 想象一下,我有一个1D数组,未分类。 If I sort the array, I lose the original index. 如果我对数组进行排序,则会丢失原始索引。 I would like to create a second element of my array to hold the original index (this I can do) then sort by first element, then binary search over the first elements. 我想创建我的数组的第二个元素来保存原始索引(我可以做)然后按第一个元素排序,然后对第一个元素进行二元搜索。

If anyone could push me in the right direction I'd be very grateful. 如果有人能把我推向正确的方向,我将非常感激。 Thanks 谢谢

Well, if I understand you correctly, you need something like this: 好吧,如果我理解正确,你需要这样的东西:

// initialize the array and the indexes array
var a2D = new int[2][];
a2D[0] = new[] { 3, 14, 15, 92, 65, 35 }; // <-- your array (fake data here)
a2D[1] = Enumerable.Range(0, a2D[0].Length).ToArray(); // create the indexes row

// sort the first row and the second one containing the indexes
Array.Sort(a2D[0], a2D[1]);

// now a2D array contains:
//  row 0: 3, 14, 15, 35, 65, 92
//  row 1: 0,  1,  2,  5,  4,  3

// and you can perform binary search on the first row:
int columnIndexOf35 = Array.BinarySearch(a2D[0], 35);
// columnIndexOf35 = 3
// 
// a2D[0][columnIndexOf35] = 35 <- value
// a2D[1][columnIndexOf35] = 5  <- original index

As per MSDN , Array.BinarySearch method operates only with one-dimensional arrays, so it is impossible to use it directly in your case. 根据MSDNArray.BinarySearch方法仅对一维数组进行操作,因此在您的情况下不可能直接使用它。 Some of the options you have are: 您拥有的一些选项是:

  1. Extract first column into a separate array and call Array.BinarySearch on it. 将第一列提取到单独的数组中并在其上调用Array.BinarySearch
  2. Define custom class Pair that implements interface IComparable and construct your array with the instances of this class. 定义实现接口IComparable自定义类Pair,并使用此类的实例构造数组。
  3. Implement binary search on two dimensional array by yourself. 自己在二维数组上实现二进制搜索。

It looks like you want to have object that holds data and "original index" and than sort/search array of objects by data. 看起来你想拥有保存数据和“原始索引”的对象,而不是按数据排序/搜索对象数组。

(This answer shows Andrei's option 2) (这个答案显示了Andrei的选择2)

class IndexedData:IComparable
{
  public MyType Data;
  public int OriginalIndex;

  public int CompareTo(object obj) {
    // add correct checks for null,.. here
    // and return correct comparison result. 
    // I.e. if MyType is IComparable - just delegate.
    return Data.CompareTo(obj);
}

Check IComparable on MSDN for implementation/usage details. 检查MSDN上的IComparable以获取实现/使用详细信息。

Depending on what you're planning to do with the arrays afterwards, another solution might be to use LINQ. 根据您之后计划对阵列执行的操作,另一种解决方案可能是使用LINQ。

var unsortedStartingArray = new[] {3, 6, 2, 1, 20, 20};
var q = unsortedStartingArray
        .Select((item, index) => new {item, index})
        .ToLookup(x => x.item, x => x.index);

var notFound = q[30]; // An empty array. Nothing found
var indexOf1 = q[1].First(); // returns 3
var multipleIndexsOf20 = q[20]; // Returns an array with 4, 5

The index into the lookup would then be the value you're searching for. 然后,查找的索引将是您要搜索的值。 Performance wise I would guesstimate this to be faster aswell about 5 times slower from my crude testing. 性能方面,我认为这比我的原油测试 速度快 5倍。

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

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