这里的排序网络可能是最有效的,因为参数数量较少,并且它们的编号是已知的编译时间(无循环条件)。
气泡分类很适合分类网络化。我把它扔在一起。它的工作原理是很简单的:
import std.stdio, std.string;
void bubbleSort(T...)(ref T values)
{
static if (T.length > 1)
{
foreach(I, _; T[0 .. $ - 1])
{
pragma(msg, format("[%s %s]", I, I + 1));
compareAndSwap(values[I], values[I + 1]);
}
bubbleSort(values[0 .. $ - 1]);
}
}
void compareAndSwap(T)(ref T a, ref T b)
{
import std.algorithm;
if(a > b)
swap(a, b);
}
void main()
{
int a = 10;
int b = 30;
int c = 11;
int d = 20;
int e = 4;
int f = 330;
int g = 21;
int h = 110;
shellSort(a, b, c, d, e, f, g, h);
writefln("%s %s %s %s %s %s %s %s!", a, b, c, d, e, f, g, h);
}
虽然说实话,如果这是标准库,小于10个参数任何排序网络应该写得一手。
编辑:我彻底改变了以前的算法,这实际上是非常不适应。气泡排序不是最佳,但它实际上排序算法正常。这里有一些编译指令可以查看构建的网络。
这确实很聪明。谢谢。 –
也许您应该在github问题主题中添加对此重要答案的引用,以获取更多关于为什么std.algorithm pull请求被激发的背景信息? –
尽管一个反思... - 并不是所有的元素必须是相同的类型才能在所有方向(组合)上互相'isAssignable'?我相信'!CommonType!T == void'不是一个足够的要求。 –