【发布时间】:2017-10-28 12:30:23
【问题描述】:
在 .NET Framework 的 PresentationCore.dll 中,有一个通用的 PriorityQueue<T> 类,其代码可以在 here 找到。
我写了一个小程序来测试排序,结果不是很好:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using MS.Internal;
namespace ConsoleTest {
public static class ConsoleTest {
public static void Main() {
PriorityQueue<int> values = new PriorityQueue<int>(6, Comparer<int>.Default);
Random random = new Random(88);
for (int i = 0; i < 6; i++)
values.Push(random.Next(0, 10000000));
int lastValue = int.MinValue;
int temp;
while (values.Count != 0) {
temp = values.Top;
values.Pop();
if (temp >= lastValue)
lastValue = temp;
else
Console.WriteLine("found sorting error");
Console.WriteLine(temp);
}
Console.ReadLine();
}
}
}
结果:
2789658
3411390
4618917
6996709
found sorting error
6381637
9367782
存在排序错误,如果增加样本量,排序错误的数量会按比例增加。
我做错了吗?如果不是,PriorityQueue类的代码中的bug究竟在哪里?
【问题讨论】:
-
根据源代码中的cmets,微软从2005-02-14就开始使用这个代码。我想知道像这样的错误是如何在 12 年多的时间里逃过注意的?
-
@Nat 因为微软唯一使用它的地方is here 并且有时选择较低优先级字体的字体是一个很难注意到的错误。
-
还有一个理由不使用标有
Internal的包中的东西...
标签: c# .net priority-queue