Repository navigation
Expand file tree
/
Copy pathBinarySearch2dArray.java
More file actions
132 lines (116 loc) · 5.08 KB
/
Copy pathBinarySearch2dArray.java
File metadata and controls
132 lines (116 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.thealgorithms.searches;
/**
* This class provides a method to search for a target value in a 2D sorted
* array.
* The search is performed using a combination of binary search on rows and
* columns.
* The 2D array must be strictly sorted in both rows and columns.
*
* The algorithm works by:
* 1. Performing a binary search on the middle column of the 2D array.
* 2. Depending on the value found, it eliminates rows above or below the middle
* element.
* 3. After finding or eliminating rows, it further applies binary search in the
* relevant columns.
*/
public final class BinarySearch2dArray {
private BinarySearch2dArray() {
}
/**
* Performs a binary search on a 2D sorted array to find the target value.
* The array must be sorted in ascending order in both rows and columns.
*
* @param arr The 2D array to search in.
* @param target The value to search for.
* @return An array containing the row and column indices of the target, or [-1,
* -1] if the target is not found.
*/
public static int[] binarySearch(int[][] arr, int target) {
int rowCount = arr.length;
int colCount = arr[0].length;
// Edge case: If there's only one row, search that row directly.
if (rowCount == 1) {
return binarySearch(arr, target, 0, 0, colCount - 1);
}
// Set initial boundaries for binary search on rows.
int startRow = 0;
int endRow = rowCount - 1;
int midCol = colCount / 2; // Middle column index for comparison.
// Perform binary search on rows based on the middle column.
while (startRow < endRow - 1) {
int midRow = startRow + (endRow - startRow) / 2;
// If the middle element matches the target, return its position.
if (arr[midRow][midCol] == target) {
return new int[] {midRow, midCol};
}
// If the middle element is smaller than the target, discard the upper half.
else if (arr[midRow][midCol] < target) {
startRow = midRow;
}
// If the middle element is larger than the target, discard the lower half.
else {
endRow = midRow;
}
}
// If the target wasn't found during the row search, check the middle column of
// startRow and endRow.
if (arr[startRow][midCol] == target) {
return new int[] {startRow, midCol};
}
if (arr[endRow][midCol] == target) {
return new int[] {endRow, midCol};
}
// Edge case: If there is only one column, target cannot be outside midCol.
if (colCount == 1) {
return new int[] {-1, -1};
}
// If target is smaller than the element in the left of startRow, perform a
// binary search on the left of startRow.
if (target <= arr[startRow][midCol - 1]) {
return binarySearch(arr, target, startRow, 0, midCol - 1);
}
// If target is between midCol and the last column of startRow, perform a binary
// search on that part of the row.
if (target >= arr[startRow][midCol + 1] && target <= arr[startRow][colCount - 1]) {
return binarySearch(arr, target, startRow, midCol + 1, colCount - 1);
}
// If target is smaller than the element in the left of endRow, perform a binary
// search on the left of endRow.
if (target <= arr[endRow][midCol - 1]) {
return binarySearch(arr, target, endRow, 0, midCol - 1);
} else {
// Otherwise, search on the right of endRow.
return binarySearch(arr, target, endRow, midCol + 1, colCount - 1);
}
}
/**
* Performs a binary search on a specific row of the 2D array.
*
* @param arr The 2D array to search in.
* @param target The value to search for.
* @param row The row index where the target will be searched.
* @param colStart The starting column index for the search.
* @param colEnd The ending column index for the search.
* @return An array containing the row and column indices of the target, or [-1,
* -1] if the target is not found.
*/
static int[] binarySearch(int[][] arr, int target, int row, int colStart, int colEnd) {
// Perform binary search within the specified column range.
while (colStart <= colEnd) {
int midIndex = colStart + (colEnd - colStart) / 2;
// If the middle element matches the target, return its position.
if (arr[row][midIndex] == target) {
return new int[] {row, midIndex};
}
// If the middle element is smaller than the target, move to the right half.
else if (arr[row][midIndex] < target) {
colStart = midIndex + 1;
}
// If the middle element is larger than the target, move to the left half.
else {
colEnd = midIndex - 1;
}
}
return new int[] {-1, -1}; // Target not found
}
}