我想生成集合(集合)的所有排列,如下所示:
Collection: 1, 2, 3
Permutations: {1, 2, 3}
{1, 3, 2}
{2, 1, 3}
{2, 3, 1}
{3, 1, 2}
{3, 2, 1}
一般来说,这不是“如何”的问题,而是关于如何最有效的问题。另外,我不想生成所有排列并返回它们,而是一次只生成一个排列,并且仅在必要时继续(很像迭代器 - 我也尝试过,但结果更少高效的)。
我测试了许多算法和方法,并提出了这段代码,这是我尝试过的最有效的代码:
public static bool NextPermutation<T>(T[] elements) where T : IComparable<T>
{
// More efficient to have a variable instead of accessing a property
var count = elements.Length;
// Indicates whether this is the last lexicographic permutation
var done = true;
// Go through the array from last to first
for (var i = count - 1; i > 0; i--)
{
var curr = elements[i];
// Check if the current element is less than the one before it
if (curr.CompareTo(elements[i - 1]) < 0)
{
continue;
}
// An element bigger than the one before it has been found,
// so this isn't the last lexicographic permutation.
done = false;
// Save the previous (bigger) element in a variable for more efficiency.
var prev = elements[i - 1];
// Have a variable to hold the index of the element to swap
// with the previous element (the to-swap element would be
// the smallest element that comes after the previous element
// and is bigger than the previous element), initializing it
// as the current index of the current item (curr).
var currIndex = i;
// Go through the array from the element after the current one to last
for (var j = i + 1; j < count; j++)
{
// Save into variable for more efficiency
var tmp = elements[j];
// Check if tmp suits the "next swap" conditions:
// Smallest, but bigger than the "prev" element
if (tmp.CompareTo(curr) < 0 && tmp.CompareTo(prev) > 0)
{
curr = tmp;
currIndex = j;
}
}
// Swap the "prev" with the new "curr" (the swap-with element)
elements[currIndex] = prev;
elements[i - 1] = curr;
// Reverse the order of the tail, in order to reset it's lexicographic order
for (var j = count - 1; j > i; j--, i++)
{
var tmp = elements[j];
elements[j] = elements[i];
elements[i] = tmp;
}
// Break since we have got the next permutation
// The reason to have all the logic inside the loop is
// to prevent the need of an extra variable indicating "i" when
// the next needed swap is found (moving "i" outside the loop is a
// bad practice, and isn't very readable, so I preferred not doing
// that as well).
break;
}
// Return whether this has been the last lexicographic permutation.
return done;
}
它的用法是发送一个元素数组,并返回一个布尔值,指示这是否是最后一个字典排列,以及将数组更改为下一个排列。
使用示例:
var arr = new[] {1, 2, 3};
PrintArray(arr);
while (!NextPermutation(arr))
{
PrintArray(arr);
}
问题是我对代码的速度不满意。
遍历大小为 11 的数组的所有排列大约需要 4 秒。虽然它可以被认为是令人印象深刻的,因为一组大小为 11 的可能排列的数量11!
接近 4000 万。
从逻辑上讲,对于大小为 12 的数组,它将花费大约 12 倍的时间,因为12!
is 11! * 12
,对于大小为 13 的数组,它将花费大约 13 倍于大小为 12 的时间,依此类推。
所以你可以很容易地理解一个大小为 12 或更大的数组,它确实需要很长时间才能完成所有排列。
而且我有一种强烈的预感,我可以以某种方式将时间缩短很多(无需切换到 C# 以外的语言 - 因为编译器优化确实可以很好地优化,我怀疑我可以在汇编中手动优化)。
有谁知道任何其他方法可以更快地完成这项工作?您对如何使当前算法更快有任何想法吗?
请注意,我不想使用外部库或服务来做到这一点 - 我希望拥有代码本身,并且我希望它尽可能高效。