Skip to content

Commit d5f1404

Browse files
authored
Add two algorithms with matrixes (TheAlgorithms#3364)
1 parent 351e85d commit d5f1404

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.thealgorithms.others;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class PrintAMatrixInSpiralOrder {
7+
/**
8+
* Search a key in row and column wise sorted matrix
9+
*
10+
* @param matrix matrix to be searched
11+
* @param row number of rows matrix has
12+
* @param col number of columns matrix has
13+
* @author Sadiul Hakim : https://github.com/sadiul-hakim
14+
*/
15+
16+
public List<Integer> print(int[][] matrix, int row, int col) {
17+
18+
// r traverses matrix row wise from first
19+
int r = 0;
20+
// c traverses matrix column wise from first
21+
int c = 0;
22+
int i;
23+
24+
List<Integer> result = new ArrayList<>();
25+
26+
while (r < row && c < col) {
27+
// print first row of matrix
28+
for (i = c; i < col; i++) {
29+
result.add(matrix[r][i]);
30+
}
31+
32+
// increase r by one because first row printed
33+
r++;
34+
35+
// print last column
36+
for (i = r; i < row; i++) {
37+
result.add(matrix[i][col - 1]);
38+
}
39+
40+
// decrease col by one because last column has been printed
41+
col--;
42+
43+
// print rows from last except printed elements
44+
if (r < row) {
45+
for (i = col - 1; i >= c; i--) {
46+
result.add(matrix[row - 1][i]);
47+
}
48+
49+
row--;
50+
51+
}
52+
53+
// print columns from first except printed elements
54+
if (c < col) {
55+
for (i = row - 1; i >= r; i--) {
56+
result.add(matrix[i][c]);
57+
}
58+
c++;
59+
}
60+
61+
}
62+
return result;
63+
}
64+
65+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.thealgorithms.searches;
2+
3+
import java.util.Arrays;
4+
5+
public class SearchInARowAndColWiseSortedMatrix {
6+
/**
7+
* Search a key in row and column wise sorted matrix
8+
*
9+
* @param matrix matrix to be searched
10+
* @param value Key being searched for
11+
* @author Sadiul Hakim : https://github.com/sadiul-hakim
12+
*/
13+
14+
public int[] search(int[][] matrix, int value) {
15+
int n = matrix.length;
16+
// This variable iterates over rows
17+
int i = 0;
18+
// This variable iterates over columns
19+
int j = n - 1;
20+
int[] result = { -1, -1 };
21+
22+
while (i < n && j >= 0) {
23+
if (matrix[i][j] == value) {
24+
result[0] = i;
25+
result[1] = j;
26+
return result;
27+
}
28+
if (value > matrix[i][j]) {
29+
i++;
30+
} else {
31+
j--;
32+
}
33+
34+
}
35+
return result;
36+
}
37+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.thealgorithms.others;
2+
3+
import java.util.List;
4+
5+
import org.junit.jupiter.api.Test;
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
public class TestPrintMatrixInSpiralOrder {
9+
@Test
10+
public void testOne() {
11+
int[][] matrix = {
12+
{ 3, 4, 5, 6, 7 },
13+
{ 8, 9, 10, 11, 12 },
14+
{ 14, 15, 16, 17, 18 },
15+
{ 23, 24, 25, 26, 27 },
16+
{ 30, 31, 32, 33, 34 }
17+
};
18+
var printClass = new PrintAMatrixInSpiralOrder();
19+
List<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
20+
List<Integer> list = List.of(3, 4, 5, 6, 7, 12, 18, 27, 34, 33, 32, 31, 30, 23, 14, 8, 9, 10, 11, 17, 26, 25,
21+
24, 15, 16);
22+
assertIterableEquals(res, list);
23+
}
24+
25+
@Test
26+
public void testTwo() {
27+
int[][] matrix = {
28+
{ 2, 2 }
29+
};
30+
var printClass = new PrintAMatrixInSpiralOrder();
31+
List<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
32+
List<Integer> list = List.of(2, 2);
33+
assertIterableEquals(res, list);
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class TestSearchInARowAndColWiseSortedMatrix {
7+
@Test
8+
public void searchItem() {
9+
int[][] matrix = {
10+
{ 3, 4, 5, 6, 7 },
11+
{ 8, 9, 10, 11, 12 },
12+
{ 14, 15, 16, 17, 18 },
13+
{ 23, 24, 25, 26, 27 },
14+
{ 30, 31, 32, 33, 34 }
15+
};
16+
17+
var test = new SearchInARowAndColWiseSortedMatrix();
18+
int[] res = test.search(matrix, 16);
19+
int[] expectedResult = { 2, 2 };
20+
assertArrayEquals(expectedResult, res);
21+
}
22+
23+
@Test
24+
public void notFound() {
25+
int[][] matrix = {
26+
{ 3, 4, 5, 6, 7 },
27+
{ 8, 9, 10, 11, 12 },
28+
{ 14, 15, 16, 17, 18 },
29+
{ 23, 24, 25, 26, 27 },
30+
{ 30, 31, 32, 33, 34 }
31+
};
32+
33+
var test = new SearchInARowAndColWiseSortedMatrix();
34+
int[] res = test.search(matrix, 96);
35+
int[] expectedResult = { -1, -1 };
36+
assertArrayEquals(expectedResult, res);
37+
}
38+
}

0 commit comments

Comments
 (0)