只需像平常一样对索引数组应用 Johnson-Trotter 算法即可。然而,不是显示索引,而是在包含任意顺序的任意元素的单独数组中显示每个相应索引处的项目。
举一个简单的例子(不是用 C 语言,但想法是一样的):
from trotter import Permutations
index_permutations = Permutations(3, [0, 1, 2])
items = [3, 1, 2]
print("Indices -> Items")
for indices in index_permutations:
print("{} -> {}".format(indices, [items[i] for i in indices]))
Output:
Indices -> Items
[0, 1, 2] -> [3, 1, 2]
[0, 2, 1] -> [3, 2, 1]
[2, 0, 1] -> [2, 3, 1]
[2, 1, 0] -> [2, 1, 3]
[1, 2, 0] -> [1, 2, 3]
[1, 0, 2] -> [1, 3, 2]