완전탐색 문제에서 등장하는 순열 알고리즘
Swap 함수와 재귀함수를 이용한다.
// Swap
public void swap(char[] arr, int i, int j) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// 순열 알고리즘
public void permutation(char[] arr, int depth, int n, int r) {
if (depth == r) {
return;
}
for (int i = depth; i < n; i++) {
swap(arr, depth, i);
permutation(arr, depth + 1, n, r);
swap(arr, depth, i);
}
}
결과 출력 시 순서는 보장이 되지 않는다.