Skip to content

Commit 7872330

Browse files
authored
Update MatrixRowPermutation.java
1 parent 1f6185c commit 7872330

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
56
/**
6-
* Generate all possible permutations of the rows of a matrix.
7+
* Generates all possible permutations of the rows of a matrix.
78
* Useful for exploring different row arrangements while keeping column structure intact.
9+
*
10+
* <p>Example usage:
11+
* <pre>
12+
* int[][] matrix = {{1, 2}, {3, 4}};
13+
* List<int[][]> permutations = MatrixRowPermutation.permuteRows(matrix);
14+
* </pre>
15+
*
16+
* @author Suraj Singh Chauhan
817
*/
918
public final class MatrixRowPermutation {
1019

20+
/** Private constructor to prevent instantiation. */
1121
private MatrixRowPermutation() {}
1222

1323
/**
14-
* Generate all permutations of the rows of a matrix.
24+
* Generates all permutations of the rows of a matrix.
1525
*
16-
* @param matrix the input matrix
17-
* @return list of matrices, each representing a unique row permutation
26+
* @param matrix the input matrix; must be non-null and non-empty
27+
* @return a list of matrices, each representing a unique row permutation
1828
* @throws IllegalArgumentException if the matrix is empty
1929
* @throws NullPointerException if the matrix is null
2030
*/
@@ -30,6 +40,13 @@ public static List<int[][]> permuteRows(int[][] matrix) {
3040
return result;
3141
}
3242

43+
/**
44+
* Helper method to generate permutations recursively.
45+
*
46+
* @param matrix the matrix whose rows are being permuted
47+
* @param start the starting index for permutation
48+
* @param result the list to store all permutations
49+
*/
3350
private static void permute(int[][] matrix, int start, List<int[][]> result) {
3451
if (start == matrix.length) {
3552
int[][] copy = new int[matrix.length][];
@@ -46,6 +63,13 @@ private static void permute(int[][] matrix, int start, List<int[][]> result) {
4663
}
4764
}
4865

66+
/**
67+
* Swaps two rows in the matrix.
68+
*
69+
* @param matrix the matrix
70+
* @param i the index of the first row
71+
* @param j the index of the second row
72+
*/
4973
private static void swap(int[][] matrix, int i, int j) {
5074
int[] temp = matrix[i];
5175
matrix[i] = matrix[j];

0 commit comments

Comments
 (0)