Update:
我对这个主题很感兴趣,所以我坐下来实现了它(使用这种非常快速且节省内存的实现 https://en.wikibooks.org/wiki/Algorithm_implementation/Sorting/Radix_sort)。我也读过this one https://www.codercorner.com/RadixSortRevisited.htm(谢谢celion https://stackoverflow.com/users/224264/celion)并发现您甚至不必将浮点数拆分为尾数和指数来对其进行排序。您只需一对一地获取这些位并执行 int 排序。您只需要关心负值,这些负值必须在算法结束时相反地放在正值前面(我在算法的最后一次迭代中一步完成了这一点,以节省一些 CPU 时间)。
这是我的浮点基数排序:
public static float[] RadixSort(this float[] array)
{
// temporary array and the array of converted floats to ints
int[] t = new int[array.Length];
int[] a = new int[array.Length];
for (int i = 0; i < array.Length; i++)
a[i] = BitConverter.ToInt32(BitConverter.GetBytes(array[i]), 0);
// set the group length to 1, 2, 4, 8 or 16
// and see which one is quicker
int groupLength = 4;
int bitLength = 32;
// counting and prefix arrays
// (dimension is 2^r, the number of possible values of a r-bit number)
int[] count = new int[1 << groupLength];
int[] pref = new int[1 << groupLength];
int groups = bitLength / groupLength;
int mask = (1 << groupLength) - 1;
int negatives = 0, positives = 0;
for (int c = 0, shift = 0; c < groups; c++, shift += groupLength)
{
// reset count array
for (int j = 0; j < count.Length; j++)
count[j] = 0;
// counting elements of the c-th group
for (int i = 0; i < a.Length; i++)
{
count[(a[i] >> shift) & mask]++;
// additionally count all negative
// values in first round
if (c == 0 && a[i] < 0)
negatives++;
}
if (c == 0) positives = a.Length - negatives;
// calculating prefixes
pref[0] = 0;
for (int i = 1; i < count.Length; i++)
pref[i] = pref[i - 1] + count[i - 1];
// from a[] to t[] elements ordered by c-th group
for (int i = 0; i < a.Length; i++){
// Get the right index to sort the number in
int index = pref[(a[i] >> shift) & mask]++;
if (c == groups - 1)
{
// We're in the last (most significant) group, if the
// number is negative, order them inversely in front
// of the array, pushing positive ones back.
if (a[i] < 0)
index = positives - (index - negatives) - 1;
else
index += negatives;
}
t[index] = a[i];
}
// a[]=t[] and start again until the last group
t.CopyTo(a, 0);
}
// Convert back the ints to the float array
float[] ret = new float[a.Length];
for (int i = 0; i < a.Length; i++)
ret[i] = BitConverter.ToSingle(BitConverter.GetBytes(a[i]), 0);
return ret;
}
它比 int 基数排序稍慢,因为在函数的开头和结尾进行数组复制,其中浮点数按位复制到 int 并返回。尽管如此,整个函数仍然是 O(n)。无论如何,比像你建议的那样连续排序 3 次要快得多。我看不到太多优化空间,但如果有人这样做:请随时告诉我。
要降序排序,请在最后更改此行:
ret[i] = BitConverter.ToSingle(BitConverter.GetBytes(a[i]), 0);
to this:
ret[a.Length - i - 1] = BitConverter.ToSingle(BitConverter.GetBytes(a[i]), 0);
测量:
我设置了一些简短的测试,包含浮点数的所有特殊情况(NaN、+/-Inf、最小/最大值、0)和随机数。它的排序顺序与 Linq 或Array.Sort
对浮点数进行排序:
NaN -> -Inf -> Min -> Negative Nums -> 0 -> Positive Nums -> Max -> +Inf
所以我用大量 10M 数字进行了测试:
float[] test = new float[10000000];
Random rnd = new Random();
for (int i = 0; i < test.Length; i++)
{
byte[] buffer = new byte[4];
rnd.NextBytes(buffer);
float rndfloat = BitConverter.ToSingle(buffer, 0);
switch(i){
case 0: { test[i] = float.MaxValue; break; }
case 1: { test[i] = float.MinValue; break; }
case 2: { test[i] = float.NaN; break; }
case 3: { test[i] = float.NegativeInfinity; break; }
case 4: { test[i] = float.PositiveInfinity; break; }
case 5: { test[i] = 0f; break; }
default: { test[i] = test[i] = rndfloat; break; }
}
}
并停止了不同排序算法的时间:
Stopwatch sw = new Stopwatch();
sw.Start();
float[] sorted1 = test.RadixSort();
sw.Stop();
Console.WriteLine(string.Format("RadixSort: {0}", sw.Elapsed));
sw.Reset();
sw.Start();
float[] sorted2 = test.OrderBy(x => x).ToArray();
sw.Stop();
Console.WriteLine(string.Format("Linq OrderBy: {0}", sw.Elapsed));
sw.Reset();
sw.Start();
Array.Sort(test);
float[] sorted3 = test;
sw.Stop();
Console.WriteLine(string.Format("Array.Sort: {0}", sw.Elapsed));
输出是(更新:现在使用发布版本运行,而不是调试):
RadixSort: 00:00:03.9902332
Linq OrderBy: 00:00:17.4983272
Array.Sort: 00:00:03.1536785
大约比 Linq 快四倍多。那还不错。但还没有那么快Array.Sort
,但也没有那么糟糕。但我对此感到非常惊讶:我预计它在非常小的数组上会比 Linq 稍微慢一些。但后来我只用 20 个元素进行了测试:
RadixSort: 00:00:00.0012944
Linq OrderBy: 00:00:00.0072271
Array.Sort: 00:00:00.0002979
即使这次我的 Radixsort 也比 Linq 更快,但是way比数组排序慢。 :)
更新2:
我做了一些更多的测量,发现了一些有趣的事情:更长的组长度常数意味着更少的迭代和更多的内存使用。如果使用 16 位的组长度(仅 2 次迭代),则在对小数组进行排序时会产生巨大的内存开销,但可以击败Array.Sort
如果数组的元素数量超过 100k,即使数量不是很多。图表轴均已对数化:
(source: daubmeier.de http://daubmeier.de/philip/stackoverflow/radixsort_vs_arraysort.png)