지금까지 제시된 두 가지 주요 답변은 O(log N)"ZigZag 방법"과 O(N+M)이진 검색 방법 인 것 같습니다. 나는 두 가지 방법을 다양한 설정과 비교하여 몇 가지 테스트를 할 것이라고 생각했습니다. 세부 사항은 다음과 같습니다.
배열은 모든 테스트에서 N x N 정사각형이며 N은 125에서 8000까지 다양합니다 (제 JVM 힙이 처리 할 수있는 최대 크기). 각 배열 크기에 대해 배열에서 임의의 위치를 선택하여 단일 2. 그런 다음 3가능한 모든 곳에 (2의 오른쪽과 아래에) 배치하고 나머지 배열을 채웠습니다.1. 일부 이전 댓글 작성자는 이러한 유형의 설정이 두 알고리즘 모두에 대해 최악의 런타임을 생성 할 것이라고 생각하는 것처럼 보였습니다. 각 어레이 크기에 대해 2 (검색 대상)에 대해 100 개의 서로 다른 무작위 위치를 선택하고 테스트를 실행했습니다. 각 알고리즘에 대해 평균 실행 시간과 최악의 실행 시간을 기록했습니다. Java에서 좋은 ms 판독 값을 얻기에는 너무 빨라서 Java의 nanoTime ()을 신뢰하지 않기 때문에 항상 균일 한 편향 계수를 추가하기 위해 각 테스트를 1000 번 반복했습니다. 결과는 다음과 같습니다.

ZigZag는 평균 및 최악의 경우 모든 테스트에서 바이너리를 이겼지 만, 모두 서로 어느 정도의 크기 내에 있습니다.
다음은 Java 코드입니다.
public class SearchSortedArray2D {
static boolean findZigZag(int[][] a, int t) {
int i = 0;
int j = a.length - 1;
while (i <= a.length - 1 && j >= 0) {
if (a[i][j] == t) return true;
else if (a[i][j] < t) i++;
else j--;
}
return false;
}
static boolean findBinarySearch(int[][] a, int t) {
return findBinarySearch(a, t, 0, 0, a.length - 1, a.length - 1);
}
static boolean findBinarySearch(int[][] a, int t,
int r1, int c1, int r2, int c2) {
if (r1 > r2 || c1 > c2) return false;
if (r1 == r2 && c1 == c2 && a[r1][c1] != t) return false;
if (a[r1][c1] > t) return false;
if (a[r2][c2] < t) return false;
int rm = (r1 + r2) / 2;
int cm = (c1 + c2) / 2;
if (a[rm][cm] == t) return true;
else if (a[rm][cm] > t) {
boolean b1 = findBinarySearch(a, t, r1, c1, r2, cm - 1);
boolean b2 = findBinarySearch(a, t, r1, cm, rm - 1, c2);
return (b1 || b2);
} else {
boolean b1 = findBinarySearch(a, t, r1, cm + 1, rm, c2);
boolean b2 = findBinarySearch(a, t, rm + 1, c1, r2, c2);
return (b1 || b2);
}
}
static void randomizeArray(int[][] a, int N) {
int ri = (int) (Math.random() * N);
int rj = (int) (Math.random() * N);
a[ri][rj] = 2;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == ri && j == rj) continue;
else if (i > ri || j > rj) a[i][j] = 3;
else a[i][j] = 1;
}
}
}
public static void main(String[] args) {
int N = 8000;
int[][] a = new int[N][N];
int randoms = 100;
int repeats = 1000;
long start, end, duration;
long zigMin = Integer.MAX_VALUE, zigMax = Integer.MIN_VALUE;
long binMin = Integer.MAX_VALUE, binMax = Integer.MIN_VALUE;
long zigSum = 0, zigAvg;
long binSum = 0, binAvg;
for (int k = 0; k < randoms; k++) {
randomizeArray(a, N);
start = System.currentTimeMillis();
for (int i = 0; i < repeats; i++) findZigZag(a, 2);
end = System.currentTimeMillis();
duration = end - start;
zigSum += duration;
zigMin = Math.min(zigMin, duration);
zigMax = Math.max(zigMax, duration);
start = System.currentTimeMillis();
for (int i = 0; i < repeats; i++) findBinarySearch(a, 2);
end = System.currentTimeMillis();
duration = end - start;
binSum += duration;
binMin = Math.min(binMin, duration);
binMax = Math.max(binMax, duration);
}
zigAvg = zigSum / randoms;
binAvg = binSum / randoms;
System.out.println(findZigZag(a, 2) ?
"Found via zigzag method. " : "ERROR. ");
System.out.println("max search time: " + zigMax + "ms");
System.out.println("avg search time: " + zigAvg + "ms");
System.out.println();
System.out.println(findBinarySearch(a, 2) ?
"Found via binary search method. " : "ERROR. ");
System.out.println("max search time: " + binMax + "ms");
System.out.println("avg search time: " + binAvg + "ms");
}
}
[[1 1][1 1]]있는가?