[英]Searching an element in a 'circular sorted' 2d array
For the past few days I've been trying to find a way to efficiently binary search this kind of array and get it to work on any given 2d array of this kind.在过去的几天里,我一直在尝试找到一种方法来有效地对这种数组进行二进制搜索,并让它在任何给定的这种二维数组上工作。
Instructions: In this question, you will refer to quadratic two-dimensional arrays, that is, the number of rows and columns is equal to the number of rows and columns equal to n.说明:在本题中,您将参考二次二维数组,即行数和列数等于行数和列数等于n。 defining a split into four quarters of n/2 size as follows:
将拆分定义为 n/2 大小的四分之四,如下所示:
Given this kind of array, I was asked to search an element with logarithmic time complexity, and if it was found I should print the row and column of the element in the array.给定这种数组,我被要求搜索具有对数时间复杂度的元素,如果找到,我应该打印数组中元素的行和列。
Does anyone have a solution for this?有人对此有解决方案吗? A full solution would be amazing, but any answer would be appreciated.
一个完整的解决方案将是惊人的,但任何答案将不胜感激。
I will share the piece of code that is very messy but this is where I got so far and it works for most of the array elements, which is not good enough since it means whole logic is wrong.我将分享一段非常混乱的代码,但这是我到目前为止的工作,它适用于大多数数组元素,这还不够好,因为这意味着整个逻辑是错误的。
int[][] mat = {
{1, 2, 3, 4, 17, 18, 19, 20},
{8, 7, 6, 5, 24, 23, 22, 21},
{12, 11, 10, 9, 28, 27, 26, 25},
{16, 15, 14, 13, 32, 31, 30, 29},
{49, 50, 51, 52, 33, 34, 35, 36},
{56, 55, 54, 53, 40, 39, 38, 37},
{60, 59, 58, 57, 45, 46, 41, 42},
{64, 63, 62, 61, 48, 47, 44, 43}};
public static boolean search(int[][] mat, int num) {
int n = mat.length;
int i = 0;
int j = 0;
int[] indic = {-1, -1};
if (num > mat[n - 1][0] || num < mat[0][0]) {
return false;
}
while (n > 1) {
int minS1 = mat[i][j];
int maxS1 = mat[(n / 2) - 1 + i][j];
int minS2 = mat[i][(n / 2) + j];
int maxS2 = mat[(n / 2) - 1 + i][(n / 2) + j];
int minS3 = mat[(n / 2) + i][(n / 2) + j];
int maxS3 = mat[(n - 1) + i][(n / 2) + j];
int minS4 = mat[(n / 2) + i][j];
int maxS4 = mat[(n - 1) + i][j];
checkSquare(num, n, indic, i, j, minS1, minS2, minS3, minS4, maxS1, maxS2, maxS3, maxS4);
if (indic[0] != -1 && indic[1] != -1) {
System.out.println("num=" + num);
System.out.println("row=" + indic[0]);
System.out.println("col=" + indic[1]);
return true;
}
boolean x = false;
if (num > maxS2) {
if (num > maxS3) {
i += n / 2;
} else {
if (n <= mat.length / 2 && i <= (n / 2)) {
x = true;
i += 1;
j += 1;
} else {
if (i >= n / 2 && j < n / 2 && ((i < n) && (j < n))) {
i += 1;
j += 1;
} else {
System.out.println(minS1);
System.out.println(minS2);
System.out.println(minS3);
i += 1;
j += 1;
}
}
}
} else {
if (num > maxS1) {
j += n / 2;
} else {
if (num > minS2) {
if (j > (mat.length / 2))
j = 0;
j += 1;
x = true;
} else j += 1;
}
}
if (num == mat[i][j]) {
System.out.println("num=" + num);
System.out.println("row=" + i);
System.out.println("col=" + j);
return true;
}
if (!x)
n = (n / 2);
}
return false;
}
I don't think the 2D array you have given is correct according to the picture you've provided.根据您提供的图片,我认为您提供的二维数组不正确。 All values in a higher quadrant (numbered clock-wise, starting from the top left) need to be bigger than all values in a lower quadrant.
较高象限中的所有值(从左上角开始顺时针编号)需要大于下象限中的所有值。 And this rule also applies for all sub-arrays until a size of 1x1 is reached.
并且此规则也适用于所有子数组,直到达到 1x1 的大小。 For example:
例如:
Element 24 is placed in position 2-1-3, meaning position 3 in the first sub-quadrant of the second main-quadrant.元素 24 位于位置 2-1-3,即第二主象限的第一子象限中的位置 3。
Likewise, element 19 is placed in position 2-2-1, so after 2-1-3同样,元素 19 被放置在位置 2-2-1,所以在 2-1-3 之后
I hope I understood correctly.我希望我理解正确。
public static boolean search (int [][] mat, int num)
{
int size=mat.length;
int currentQuadrantRow=0;
int currentQuadrantCol=0;
int minQ1=mat[0][0];
int minQ2=mat[0][size/2];
int minQ3=mat[size/2][size/2];
int minQ4=mat[size/2][0];
int maxQ1;
int maxQ2;
int maxQ3;
int maxQ4;
if (size / 2 == 0)
{
maxQ1 = minQ1;
maxQ2 = minQ2;
maxQ3=minQ3;
maxQ4=minQ4;
}
else
{
maxQ1 = mat[(size/ 2)-1][0];
maxQ2 = mat[((size/ 2) - 1)][0+(size / 2)];
maxQ3 = mat[(size - 1)][0+(size / 2)];
maxQ4 = mat[(size - 1)][0];
}
while (size>1)
{
if (minQ1 > num) {
return false;
} else if (num <= maxQ1) {
size = size/ 2;
} else if (minQ2 > num) {
return false;
} else if (num <= maxQ2) {
size = size/ 2;
currentQuadrantCol = currentQuadrantCol + size;
} else if (minQ3 > num) {
return false;
} else if (num <= maxQ3) {
size = size/ 2;
currentQuadrantRow = currentQuadrantRow + size;
currentQuadrantCol = currentQuadrantCol + size;
} else if (minQ4 > num) {
return false;
} else if (num <= maxQ4) {
size = size/ 2;
currentQuadrantRow = currentQuadrantRow + size;
} else if (num > maxQ4) {
return false;
}
System.out.println(currentQuadrantRow);
System.out.println(currentQuadrantCol);
minQ1 = mat[currentQuadrantRow][currentQuadrantCol];
minQ2 = mat[currentQuadrantRow][currentQuadrantCol+(size / 2)];
minQ3 = mat[currentQuadrantRow+(size / 2)][currentQuadrantCol+(size / 2)];
minQ4 = mat[currentQuadrantRow+(size / 2)][currentQuadrantCol];
if (size / 2 == 0)
{
maxQ1 = minQ1;
maxQ2 = minQ2;
maxQ3=minQ3;
maxQ4=minQ4;
}
else
{
maxQ1 = mat[currentQuadrantRow+((size/ 2) - 1)][currentQuadrantCol];
maxQ2 = mat[currentQuadrantRow+((size/ 2) - 1)][currentQuadrantCol+(size / 2)];
maxQ3 = mat[currentQuadrantRow+(size - 1)][currentQuadrantCol+(size / 2)];
maxQ4 = mat[currentQuadrantRow+(size - 1)][currentQuadrantCol];
}
}
if(mat[currentQuadrantRow][currentQuadrantCol]==num)
{
return true;
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.