Skip to content

Commit 6d18a93

Browse files
committed
refactor: simplify SearchSortedMatrix loop guards
1 parent ca4145d commit 6d18a93

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/main/java/com/thealgorithms/matrix/SearchSortedMatrix.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,16 @@ private SearchSortedMatrix() {
6969
* @throws NullPointerException if {@code comparator} is null
7070
*/
7171
public static <T> boolean search(final T[][] matrix, final T target, final Comparator<? super T> comparator) {
72-
if (matrix == null || matrix.length == 0) {
72+
if (matrix == null) {
7373
return false;
7474
}
75-
if (matrix[0] == null || matrix[0].length == 0) {
75+
if (matrix.length == 0) {
76+
return false;
77+
}
78+
if (matrix[0] == null) {
79+
return false;
80+
}
81+
if (matrix[0].length == 0) {
7682
return false;
7783
}
7884

@@ -93,7 +99,10 @@ public static <T> boolean search(final T[][] matrix, final T target, final Compa
9399
int rowIndex = 0;
94100
int colIndex = colCount - 1;
95101

96-
while (rowIndex < rowCount && colIndex >= 0) {
102+
while (rowIndex < rowCount) {
103+
if (colIndex < 0) {
104+
break;
105+
}
97106
final T value = matrix[rowIndex][colIndex];
98107
final int comparison = comparator.compare(value, target);
99108
if (comparison == 0) {
@@ -120,10 +129,16 @@ public static <T> boolean search(final T[][] matrix, final T target, final Compa
120129
* @throws IllegalArgumentException if the matrix is jagged or contains null rows
121130
*/
122131
public static boolean search(final int[][] matrix, final int target) {
123-
if (matrix == null || matrix.length == 0) {
132+
if (matrix == null) {
124133
return false;
125134
}
126-
if (matrix[0] == null || matrix[0].length == 0) {
135+
if (matrix.length == 0) {
136+
return false;
137+
}
138+
if (matrix[0] == null) {
139+
return false;
140+
}
141+
if (matrix[0].length == 0) {
127142
return false;
128143
}
129144

@@ -142,7 +157,10 @@ public static boolean search(final int[][] matrix, final int target) {
142157
int rowIndex = 0;
143158
int colIndex = colCount - 1;
144159

145-
while (rowIndex < rowCount && colIndex >= 0) {
160+
while (rowIndex < rowCount) {
161+
if (colIndex < 0) {
162+
break;
163+
}
146164
final int value = matrix[rowIndex][colIndex];
147165
if (value == target) {
148166
return true;

src/test/java/com/thealgorithms/matrix/SearchSortedMatrixTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ void genericSearchFindsExistingTarget() {
6969
assertFalse(SearchSortedMatrix.search(matrix, 20, Comparator.naturalOrder()));
7070
}
7171

72+
@Test
73+
void genericTargetSmallerThanAllValuesReturnsFalse() {
74+
final Integer[][] matrix = {
75+
{1, 4},
76+
{2, 5},
77+
};
78+
79+
assertFalse(SearchSortedMatrix.search(matrix, 0, Comparator.naturalOrder()));
80+
}
81+
7282
@Test
7383
void genericSearchCoversAllComparisonBranches() {
7484
final Integer[][] matrix = {

0 commit comments

Comments
 (0)