简体   繁体   English

在循环排序的二维矩阵中搜索元素

[英]Search for an element in a circular sorted 2d matrix

I was given a question in my class that I just can't get my head around it.我在 class 中收到了一个问题,我无法理解它。 I've been trying for days and can't think of a logic that works 100%我已经尝试了好几天,想不出一个 100% 有效的逻辑

The question is: Search for an element in a circular sorted 2d matrix, example below:问题是:在循环排序的 2d 矩阵中搜索元素,示例如下: 圆形排序二维矩阵

We were asked to do it with the best complexity available.我们被要求以可用的最佳复杂性来做这件事。 I've figured out that the rows are sorted by size, But this theory failed.我发现行是按大小排序的,但是这个理论失败了。

I've figured if I'll find the middle element, I could move the pointer accordingly, but it makes no sense.我想如果我能找到中间元素,我可以相应地移动指针,但这没有任何意义。

I was thinking that maybe I should find each quadrant's start index and last index, and search inside, but I don't know how to do that with a 2d array我在想也许我应该找到每个象限的开始索引和最后一个索引,然后在里面搜索,但我不知道如何用二维数组来做到这一点

Here's my piece of code that I crafted so far (It's useless)这是我到目前为止编写的一段代码(没用)

 public static boolean search (int [][] mat, int num) {
    int midRowIndex =(mat.length-1)/2;
    int midColIndex =(mat[0].length-1)/2;
    int start = 0;
    int end =(mat.length*mat[0].length)-1;
    System.out.println("Mid index is: [" +midColIndex + "][" + midRowIndex + "]");
    System.out.println("Number of indexes is: " +end);
    System.out.println("Searching for: " +num );
    while (start<end) {
        if (mat[midRowIndex][midColIndex] == num)
            return true;
        if (mat[midRowIndex][midColIndex] < num)
            if(midColIndex < mat.length)
                midColIndex+=1;
            else{
                midColIndex=0;
                midRowIndex+=1;
            }
        if (mat[midRowIndex][midColIndex] > num)
            midRowIndex-=1;
        start+=1;
    }
    return false;
}

I'm not asking for a direct answer, Just for some leads.我不是要直接回答,只是为了一些线索。

Thanks!谢谢!

what you can do here is a variation of binary search.你可以在这里做的是二进制搜索的一种变体。

Here is the algorithm (maybe it has minor mistakes but this is the general idea):这是算法(也许它有一些小错误,但这是一般的想法):

For matrix a of size n x n, and searched value s:

evenIteration = true //as in even/odd
startX, startY = 0
endX, endY = n

while(endX >= startX && endY >= startY):


middleY = ((endY + startY)/2)
middleX = ((endX + startX)/2)

if evenIteration:
  i = middleY - 1
  j = middleX
Else:
  i = endY - 1
  j = startX

candidate = a[i][j]

if s  == candidate:
  Return i,j // We found the searched value!

if s < candidate:
  if evenIteration:
    endY = middleY
  else:
    endX = middleX
else:
  if evenIteration:
    startY = middleY
  else:
    startX = middleX

evenIteration = !evenIteration

Try understanding the logic behind this, and implement this in code.尝试理解这背后的逻辑,并在代码中实现它。 Write unit tests and debug the code.编写单元测试并调试代码。 this would help you understand the algorithm better.这将帮助您更好地理解算法。

Let me know in the comments if you need further explanation如果您需要进一步解释,请在评论中告诉我

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

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