44import java .util .List ;
55
66/**
7- * Generates all possible permutations of the rows of a matrix.
8- * 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>
7+ * This class provides a method to generate all possible permutations of the rows of a matrix.
8+ * Row permutations are useful for exploring different arrangements while keeping the column structure intact.
9+ *
10+ * For example, consider the following 2x2 matrix:
11+ * 1 2
12+ * 3 4
13+ * The row permutations are:
14+ * 1 2 3 4
15+ * 3 4 1 2
1516 *
1617 * @author Suraj Singh Chauhan
1718 */
1819public final class MatrixRowPermutation {
1920
20- /** Private constructor to prevent instantiation. */
21- private MatrixRowPermutation () { }
21+ private MatrixRowPermutation () {
22+ }
2223
2324 /**
24- * Generates all permutations of the rows of a matrix.
25+ * @brief Generates all permutations of the rows of a matrix.
2526 *
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
27+ * @param matrix The input matrix; must be non-null and non-empty
28+ * @return A list of matrices, each representing a unique row permutation
2829 * @throws IllegalArgumentException if the matrix is empty
2930 * @throws NullPointerException if the matrix is null
3031 */
@@ -35,17 +36,18 @@ public static List<int[][]> permuteRows(int[][] matrix) {
3536 if (matrix .length == 0 ) {
3637 throw new IllegalArgumentException ("Matrix is empty" );
3738 }
39+
3840 List <int [][]> result = new ArrayList <>();
3941 permute (matrix , 0 , result );
4042 return result ;
4143 }
4244
4345 /**
44- * Helper method to generate permutations recursively .
46+ * @brief Helper method to recursively generate permutations of matrix rows .
4547 *
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
48+ * @param matrix The matrix being permuted
49+ * @param start The current index to fix
50+ * @param result The list to store all permutations
4951 */
5052 private static void permute (int [][] matrix , int start , List <int [][]> result ) {
5153 if (start == matrix .length ) {
@@ -64,11 +66,11 @@ private static void permute(int[][] matrix, int start, List<int[][]> result) {
6466 }
6567
6668 /**
67- * Swaps two rows in the matrix.
69+ * @brief Swaps two rows in the matrix.
6870 *
69- * @param matrix the matrix
70- * @param i the index of the first row
71- * @param j the index of the second row
71+ * @param matrix The matrix
72+ * @param i The index of the first row
73+ * @param j The index of the second row
7274 */
7375 private static void swap (int [][] matrix , int i , int j ) {
7476 int [] temp = matrix [i ];
0 commit comments