import java.util.Arrays;
public class Permutation1 {
public static void permute(int[] arr, int depth, int n) {
if (depth == n) {
System.out.println(Arrays.toString(arr)); // 순열 출력
System.out.println();
return;
}
for (int i = depth; i < n; i++) {
System.out.println("SWAP depth="+depth+ ", i="+ i);
swap(arr, depth, i);
permute(arr, depth + 1, n);
swap(arr, depth, i); // 원상 복구
}
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
int[] arr = { 1, 2, 3 };
permute(arr, 0, arr.length);
}
}