Skip to content

Commit 7b934af

Browse files
authored
Improve comments, add function documentation in BinarySearch2dArray.java (#5518)
1 parent ce6d98f commit 7b934af

File tree

1 file changed

+67
-27
lines changed

1 file changed

+67
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,127 @@
11
package com.thealgorithms.searches;
22

3-
/*
4-
To apply this method, the provided array must be strictly sorted. In this method, two pointers, one
5-
at 0th row & the other at the last row are taken & the searching is done on the basis of the middle
6-
element of the middle column. If that element is equal to target, its coordinates are returned, else
7-
if it is smaller than the target, the rows above that element are ignored (because the elements
8-
above it will also be smaller than the target), else that element is greater than the target, then
9-
the rows below it are ignored.
3+
/**
4+
* This class provides a method to search for a target value in a 2D sorted
5+
* array.
6+
* The search is performed using a combination of binary search on rows and
7+
* columns.
8+
* The 2D array must be strictly sorted in both rows and columns.
9+
*
10+
* The algorithm works by:
11+
* 1. Performing a binary search on the middle column of the 2D array.
12+
* 2. Depending on the value found, it eliminates rows above or below the middle
13+
* element.
14+
* 3. After finding or eliminating rows, it further applies binary search in the
15+
* relevant columns.
1016
*/
1117
public final class BinarySearch2dArray {
18+
1219
private BinarySearch2dArray() {
1320
}
1421

22+
/**
23+
* Performs a binary search on a 2D sorted array to find the target value.
24+
* The array must be sorted in ascending order in both rows and columns.
25+
*
26+
* @param arr The 2D array to search in.
27+
* @param target The value to search for.
28+
* @return An array containing the row and column indices of the target, or [-1,
29+
* -1] if the target is not found.
30+
*/
1531
static int[] binarySearch(int[][] arr, int target) {
1632
int rowCount = arr.length;
1733
int colCount = arr[0].length;
1834

35+
// Edge case: If there's only one row, search that row directly.
1936
if (rowCount == 1) {
2037
return binarySearch(arr, target, 0, 0, colCount);
2138
}
2239

40+
// Set initial boundaries for binary search on rows.
2341
int startRow = 0;
2442
int endRow = rowCount - 1;
25-
int midCol = colCount / 2;
43+
int midCol = colCount / 2; // Middle column index for comparison.
2644

45+
// Perform binary search on rows based on the middle column.
2746
while (startRow < endRow - 1) {
28-
int midRow = startRow + (endRow - startRow) / 2; // getting the index of middle row
47+
int midRow = startRow + (endRow - startRow) / 2;
2948

49+
// If the middle element matches the target, return its position.
3050
if (arr[midRow][midCol] == target) {
3151
return new int[] {midRow, midCol};
32-
} else if (arr[midRow][midCol] < target) {
52+
}
53+
// If the middle element is smaller than the target, discard the upper half.
54+
else if (arr[midRow][midCol] < target) {
3355
startRow = midRow;
34-
} else {
56+
}
57+
// If the middle element is larger than the target, discard the lower half.
58+
else {
3559
endRow = midRow;
3660
}
3761
}
38-
/*
39-
if the above search fails to find the target element, these conditions will be used to
40-
find the target element, which further uses the binary search algorithm in the places
41-
which were left unexplored.
42-
*/
62+
63+
// If the target wasn't found during the row search, check the middle column of
64+
// startRow and endRow.
4365
if (arr[startRow][midCol] == target) {
44-
return new int[] {
45-
startRow,
46-
midCol,
47-
};
66+
return new int[] {startRow, midCol};
4867
}
4968

5069
if (arr[endRow][midCol] == target) {
5170
return new int[] {endRow, midCol};
5271
}
5372

73+
// If target is smaller than the element in the left of startRow, perform a
74+
// binary search on the left of startRow.
5475
if (target <= arr[startRow][midCol - 1]) {
5576
return binarySearch(arr, target, startRow, 0, midCol - 1);
5677
}
5778

79+
// If target is between midCol and the last column of startRow, perform a binary
80+
// search on that part of the row.
5881
if (target >= arr[startRow][midCol + 1] && target <= arr[startRow][colCount - 1]) {
5982
return binarySearch(arr, target, startRow, midCol + 1, colCount - 1);
6083
}
6184

85+
// If target is smaller than the element in the left of endRow, perform a binary
86+
// search on the left of endRow.
6287
if (target <= arr[endRow][midCol - 1]) {
6388
return binarySearch(arr, target, endRow, 0, midCol - 1);
6489
} else {
90+
// Otherwise, search on the right of endRow.
6591
return binarySearch(arr, target, endRow, midCol + 1, colCount - 1);
6692
}
6793
}
6894

95+
/**
96+
* Performs a binary search on a specific row of the 2D array.
97+
*
98+
* @param arr The 2D array to search in.
99+
* @param target The value to search for.
100+
* @param row The row index where the target will be searched.
101+
* @param colStart The starting column index for the search.
102+
* @param colEnd The ending column index for the search.
103+
* @return An array containing the row and column indices of the target, or [-1,
104+
* -1] if the target is not found.
105+
*/
69106
static int[] binarySearch(int[][] arr, int target, int row, int colStart, int colEnd) {
107+
// Perform binary search within the specified column range.
70108
while (colStart <= colEnd) {
71109
int midIndex = colStart + (colEnd - colStart) / 2;
72110

111+
// If the middle element matches the target, return its position.
73112
if (arr[row][midIndex] == target) {
74-
return new int[] {
75-
row,
76-
midIndex,
77-
};
78-
} else if (arr[row][midIndex] < target) {
113+
return new int[] {row, midIndex};
114+
}
115+
// If the middle element is smaller than the target, move to the right half.
116+
else if (arr[row][midIndex] < target) {
79117
colStart = midIndex + 1;
80-
} else {
118+
}
119+
// If the middle element is larger than the target, move to the left half.
120+
else {
81121
colEnd = midIndex - 1;
82122
}
83123
}
84124

85-
return new int[] {-1, -1};
125+
return new int[] {-1, -1}; // Target not found
86126
}
87127
}

0 commit comments

Comments
 (0)