Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
package com.thealgorithms.searches;

public class SearchInARowAndColWiseSortedMatrix {
/**
* Search a key in row and column wise sorted matrix
*
* @param matrix matrix to be searched
* @param value Key being searched for
* @author Sadiul Hakim : https://github.com/sadiul-hakim
*/
public int[] search(int[][] matrix, int value) {
int n = matrix.length;
// This variable iterates over rows
int i = 0;
// This variable iterates over columns
int j = n - 1;
int[] result = {-1, -1};
while (i < n && j >= 0) {
if (matrix[i][j] == value) {
result[0] = i;
result[1] = j;
return result;
}
if (value > matrix[i][j]) {
i++;
} else {
j--;
}
}
return result;
}
}
package com.thealgorithms.searches;

public final class SearchInARowAndColWiseSortedMatrix {
/**
* Search a key in row and column wise sorted matrix
*
* @param matrix matrix to be searched
* @param value Key being searched for
* @return row and column indices if found, otherwise {-1, -1}
* @author Sadiul Hakim : https://github.com/sadiul-hakim
*/
public int[] search(int[][] matrix, int value) {
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
return new int[] {-1, -1};
}

int rows = matrix.length;
int cols = matrix[0].length;
// This variable iterates over rows
int i = 0;
// This variable iterates over columns, starting from top-right corner
int j = cols - 1;
int[] result = {-1, -1};
while (i < rows && j >= 0) {
if (matrix[i] == null || j >= matrix[i].length) {
i++;
continue;
}
if (matrix[i][j] == value) {
result[0] = i;
result[1] = j;
return result;
}
if (value > matrix[i][j]) {
i++;
} else {
j--;
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,36 @@ void testSearchInColWiseSortedMatrix() {
int[] expected = {1, 1}; // Found at (1, 1)
assertArrayEquals(expected, searcher.search(matrix, value), "Value should be found in the column-wise sorted matrix");
}

@Test
void testSearchInNullMatrix() {
assertArrayEquals(new int[] {-1, -1}, searcher.search(null, 5), "Should return {-1, -1} for null matrix");
}

@Test
void testSearchInMatrixWithEmptyRows() {
int[][] matrix = {{}};
assertArrayEquals(new int[] {-1, -1}, searcher.search(matrix, 5), "Should return {-1, -1} for matrix with empty first row");
}

@Test
void testSearchInRectangularMatrixMoreColsThanRowsFound() {
int[][] matrix = {{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}};
int[] expected = {0, 4}; // Value 9 is at row 0, col 4
assertArrayEquals(expected, searcher.search(matrix, 9), "Value 9 should be found at row 0, col 4");
}

@Test
void testSearchInRectangularMatrixMoreRowsThanColsFound() {
int[][] matrix = {{1, 5}, {2, 6}, {3, 7}, {4, 8}};
int[] expected = {2, 0}; // Value 3 is at row 2, col 0
assertArrayEquals(expected, searcher.search(matrix, 3), "Value 3 should be found at row 2, col 0");
}

@Test
void testSearchInRectangularMatrixNotFound() {
int[][] matrix = {{1, 3, 5}, {2, 4, 6}, {7, 8, 9}, {10, 11, 12}};
int[] expected = {-1, -1};
assertArrayEquals(expected, searcher.search(matrix, 15), "Value 15 should not be found");
}
}
Loading