Ai
1 Star 0 Fork 3

aiobc/Algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Permutations.java 4.21 KB
一键复制 编辑 原始数据 按行查看 历史
Rafael Chaves 提交于 2020-02-03 02:07 +08:00 . Adopt standard Java project layout (#120)
/**
* Here we present two methods (recursive and iterative) of generating all the permutations of a
* list of elements.
*
* <p>Time Complexity: O(n!)
*
* @author William Fiset, Micah Stairs
*/
package com.williamfiset.algorithms.other;
public class Permutations {
/* RECURSIVE APPROACH */
// Generates all the permutations of a sequence of objects
public static void generatePermutations(Object[] sequence) {
if (sequence == null) return;
boolean[] used = new boolean[sequence.length];
int[] picked = new int[sequence.length];
permutations(0, used, picked, sequence);
}
// Recursive method to generate all the permutations of a sequence
// at -> Current element we're considering
// used -> The elements we have currently selected in our permutation
// picked -> The order of the indexes we have selected in our permutation
// sequence -> The array we're generating permutations for
private static void permutations(int at, boolean[] used, int[] picked, Object[] sequence) {
final int N = sequence.length;
// We reached the end, so we've found a valid permutation!
if (at == N) {
// Print permutation
System.out.print("[ ");
for (int i = 0; i < N; i++) System.out.print(sequence[picked[i]] + " ");
System.out.println("]");
} else {
for (int i = 0; i < N; i++) {
// We can only select elements once, so make sure we do
// not select an element which has already been chosen
if (!used[i]) {
// Select this element and track in picked which
// element was chosen for this permutations
used[i] = true;
picked[at] = i;
permutations(at + 1, used, picked, sequence);
// Backtrack (unselect element)
used[i] = false;
}
}
}
}
/* ITERATIVE APPROACH */
// Generates the next ordered permutation in-place (skips repeated permutations).
// Calling this when the array is already at the highest permutation returns false.
// Recommended usage is to start with the smallest permutations and use a do while
// loop to generate each successive permutations (see main for example).
static <T extends Comparable<? super T>> boolean nextPermutation(T[] sequence) {
int first = getFirst(sequence);
if (first == -1) return false;
int toSwap = sequence.length - 1;
while (sequence[first].compareTo(sequence[toSwap]) >= 0) --toSwap;
swap(sequence, first++, toSwap);
toSwap = sequence.length - 1;
while (first < toSwap) swap(sequence, first++, toSwap--);
return true;
}
static <T extends Comparable<? super T>> int getFirst(T[] sequence) {
for (int i = sequence.length - 2; i >= 0; --i)
if (sequence[i].compareTo(sequence[i + 1]) < 0) return i;
return -1;
}
static <T extends Comparable<? super T>> void swap(T[] sequence, int i, int j) {
T tmp = sequence[i];
sequence[i] = sequence[j];
sequence[j] = tmp;
}
public static void main(String[] args) {
Integer[] sequence = {1, 1, 2, 3};
generatePermutations(sequence);
// prints:
// [ 1 1 2 3 ]
// [ 1 1 3 2 ]
// [ 1 2 1 3 ]
// [ 1 2 3 1 ]
// [ 1 3 1 2 ]
// [ 1 3 2 1 ]
// [ 1 1 2 3 ]
// [ 1 1 3 2 ]
// [ 1 2 1 3 ]
// [ 1 2 3 1 ]
// [ 1 3 1 2 ]
// [ 1 3 2 1 ]
// [ 2 1 1 3 ]
// [ 2 1 3 1 ]
// [ 2 1 1 3 ]
// [ 2 1 3 1 ]
// [ 2 3 1 1 ]
// [ 2 3 1 1 ]
// [ 3 1 1 2 ]
// [ 3 1 2 1 ]
// [ 3 1 1 2 ]
// [ 3 1 2 1 ]
// [ 3 2 1 1 ]
// [ 3 2 1 1 ]
String[] alpha = {"A", "B", "C", "D"};
do {
System.out.println(java.util.Arrays.toString(alpha));
// Loop while alpha is not at its highest permutation ordering
} while (nextPermutation(alpha));
// prints:
// [A, B, C, D]
// [A, B, D, C]
// [A, C, B, D]
// [A, C, D, B]
// [A, D, B, C]
// [A, D, C, B]
// [B, A, C, D]
// [B, A, D, C]
// [B, C, A, D]
// [B, C, D, A]
// [B, D, A, C]
// [B, D, C, A]
// [C, A, B, D]
// [C, A, D, B]
// [C, B, A, D]
// [C, B, D, A]
// [C, D, A, B]
// [C, D, B, A]
// [D, A, B, C]
// [D, A, C, B]
// [D, B, A, C]
// [D, B, C, A]
// [D, C, A, B]
// [D, C, B, A]
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/aiobc/Algorithms.git
git@gitee.com:aiobc/Algorithms.git
aiobc
Algorithms
Algorithms
master

搜索帮助