Skip to content

Commit 31c6d34

Browse files
authored
Update MatrixRowPermutation.java
1 parent d460f46 commit 31c6d34

File tree

1 file changed

+6
-36
lines changed

1 file changed

+6
-36
lines changed

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

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,20 @@
44
import java.util.List;
55

66
/**
7-
*
8-
* <h1>Find All Row Permutations of a Matrix!</h1>
9-
*
10-
* This program generates and returns all possible permutations of the rows of a
11-
* given matrix.
12-
* It is useful for exploring different row arrangements while keeping column
13-
* structure intact.
14-
*
15-
* <p>
16-
* <b>Note:</b> Giving proper comments in your program makes it more user
17-
* friendly and it is assumed as a high quality code.
18-
*
19-
* @author Copilot
20-
* @version 11.0.9
21-
* @since 2025-10-01
7+
* Generate all possible permutations of the rows of a matrix.
8+
* Useful for exploring different row arrangements while keeping column structure intact.
229
*/
2310
public final class MatrixRowPermutation {
2411

25-
private MatrixRowPermutation() {
26-
}
12+
private MatrixRowPermutation() {}
2713

2814
/**
2915
* Generate all permutations of the rows of a matrix.
3016
*
31-
* @param matrix The input matrix whose row permutations are to be generated
17+
* @param matrix the input matrix
18+
* @return list of matrices, each representing a unique row permutation
3219
* @throws IllegalArgumentException if the matrix is empty
33-
* @throws NullPointerException if the matrix is null
34-
* @return A list of matrices, each representing a unique row permutation
20+
* @throws NullPointerException if the matrix is null
3521
*/
3622
public static List<int[][]> permuteRows(int[][] matrix) {
3723
if (matrix == null) {
@@ -40,19 +26,11 @@ public static List<int[][]> permuteRows(int[][] matrix) {
4026
if (matrix.length == 0) {
4127
throw new IllegalArgumentException("Matrix is empty");
4228
}
43-
4429
List<int[][]> result = new ArrayList<>();
4530
permute(matrix, 0, result);
4631
return result;
4732
}
4833

49-
/**
50-
* Helper method to recursively generate permutations.
51-
*
52-
* @param matrix The matrix being permuted
53-
* @param start The current index to fix
54-
* @param result The list to store all permutations
55-
*/
5634
private static void permute(int[][] matrix, int start, List<int[][]> result) {
5735
if (start == matrix.length) {
5836
int[][] copy = new int[matrix.length][];
@@ -62,21 +40,13 @@ private static void permute(int[][] matrix, int start, List<int[][]> result) {
6240
result.add(copy);
6341
return;
6442
}
65-
6643
for (int i = start; i < matrix.length; i++) {
6744
swap(matrix, start, i);
6845
permute(matrix, start + 1, result);
6946
swap(matrix, start, i); // backtrack
7047
}
7148
}
7249

73-
/**
74-
* Swap two rows in the matrix.
75-
*
76-
* @param matrix The matrix in which rows are to be swapped
77-
* @param i The first row index
78-
* @param j The second row index
79-
*/
8050
private static void swap(int[][] matrix, int i, int j) {
8151
int[] temp = matrix[i];
8252
matrix[i] = matrix[j];

0 commit comments

Comments
 (0)