Skip to content

Commit 16d066c

Browse files
committed
Fix
1 parent abe61e7 commit 16d066c

File tree

5 files changed

+45
-136
lines changed

5 files changed

+45
-136
lines changed

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,41 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
/**
7+
* Utility class to print a matrix in spiral order.
8+
* <p>
9+
* Given a 2D array (matrix), this class provides a method to return the
10+
* elements
11+
* of the matrix in spiral order, starting from the top-left corner and moving
12+
* clockwise.
13+
* </p>
14+
*
15+
* @author Sadiul Hakim (https://github.com/sadiul-hakim)
16+
*/
617
public class PrintAMatrixInSpiralOrder {
18+
719
/**
8-
* Search a key in row and column wise sorted matrix
20+
* Returns the elements of the given matrix in spiral order.
21+
*
22+
* @param matrix the 2D array to traverse in spiral order
23+
* @param row the number of rows in the matrix
24+
* @param col the number of columns in the matrix
25+
* @return a list containing the elements of the matrix in spiral order
926
*
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
27+
* <p>
28+
* Example:
29+
*
30+
* <pre>
31+
* int[][] matrix = {
32+
* {1, 2, 3},
33+
* {4, 5, 6},
34+
* {7, 8, 9}
35+
* };
36+
* print(matrix, 3, 3) returns [1, 2, 3, 6, 9, 8, 7, 4, 5]
37+
* </pre>
38+
* </p>
1439
*/
1540
public List<Integer> print(int[][] matrix, int row, int col) {
16-
1741
// r traverses matrix row wise from first
1842
int r = 0;
1943
// c traverses matrix column wise from first

src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/test/java/com/thealgorithms/others/PrintAMatrixInSpiralOrderTest.java renamed to src/test/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrderTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.others;
1+
package com.thealgorithms.matrix;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

@@ -66,4 +66,18 @@ void testMatrixWithNegativeNumbers() {
6666
List<Integer> expected = Arrays.asList(-1, -2, -4, -3);
6767
assertEquals(expected, spiralPrinter.print(matrix, 2, 2));
6868
}
69+
70+
@Test
71+
void testLargeSquareMatrix() {
72+
int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
73+
List<Integer> expected = Arrays.asList(3, 4, 5, 6, 7, 12, 18, 27, 34, 33, 32, 31, 30, 23, 14, 8, 9, 10, 11, 17, 26, 25, 24, 15, 16);
74+
assertEquals(expected, spiralPrinter.print(matrix, 5, 5));
75+
}
76+
77+
@Test
78+
void testSingleRowWithTwoElements() {
79+
int[][] matrix = {{2, 2}};
80+
List<Integer> expected = Arrays.asList(2, 2);
81+
assertEquals(expected, spiralPrinter.print(matrix, 1, 2));
82+
}
6983
}

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

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)