/
Permutation_Last.java
67 lines (53 loc) · 1.36 KB
/
Permutation_Last.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package Alone;
public class Permutation_Last {
public static void main(String[] args) {
int n=3;
int[] arr = {1,2,3};
int[] output = new int[n];
boolean[] visited = new boolean[n];
System.out.println("visited 배열 사용 permutation");
perm(arr, output, visited,0,n,3);
System.out.println();
System.out.println("swap 함수를 사용한 permutation");
permutation(arr,0,n,3);
}
static void permutation(int[] arr, int depth, int n, int r) {
if(depth==r) {
print(arr,r);
return;
}
for(int i=depth;i<n;i++) {
swap(arr,depth,i);
permutation(arr,depth+1,n,r);
swap(arr,depth,i);
}
}
static void swap(int[] arr, int depth, int i) {
int temp = arr[depth];
arr[depth]= arr[i];
arr[i]= temp;
}
static void perm(int[] arr, int[] output, boolean[] visited, int depth, int n, int r) {
if(depth ==r) {
print(output, r);
return;
}
for(int i=0;i<n;i++) {
/***************************************
여기서 visited 배열을 사용하지 않으면 중복 순열이 가능하다
*******************************************/
if(!visited[i]) {
visited[i]=true;
output[depth]=arr[i];
perm(arr,output,visited,depth+1,n,r);
visited[i]=false;
}
}
}
static void print(int[] arr, int r) {
for(int i=0;i<r;i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
}