-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathPermutationDemo.java
More file actions
35 lines (28 loc) · 861 Bytes
/
PermutationDemo.java
File metadata and controls
35 lines (28 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package ds_008_recursion;
public class PermutationDemo {
public static void main(String[] args) {
int numberOfChoices = 3;
int[] permutations = new int[2];
makePermutation(numberOfChoices, permutations);
}
// Helper method
private static void makePermutation(int numberOfChoices, int[] permutations) {
makePermutation(0, numberOfChoices, permutations);
}
private static void makePermutation(int index, int numberOfChoices, int[] permutations) {
if(index == permutations.length) {
printArray(permutations);
} else {
for(int i = 1; i <= numberOfChoices; i++) {
permutations[index] = i;
makePermutation(index + 1, numberOfChoices, permutations);
}
}
}
private static void printArray(int[] array) {
for(int i = 0; i < array.length; i++) {
System.out.printf("%d ", array[i]);
}
System.out.print("\n");
}
}