简体   繁体   English

NET 4.5内省式实现Array.Sort是否具有确定性?

[英]Is .NET 4.5 introspective implementation of Array.Sort deterministic?

.NET 4.5 changed the implementation of Array.Sort to what is called "introspective sort" which is a hybrid algorithm, consisting of choosing between quicksort, insertionsort, and heapsort depending on the input data. .NET 4.5将Array.Sort的实现更改为所谓的“自省排序”,这是一种混合算法,包括根据输入数据在快速排序,插入排序和堆排序之间进行选择。 It is detailed here: 在这里详细说明:

http://msdn.microsoft.com/en-us/library/6tf1f0bc(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/6tf1f0bc(v=vs.110).aspx

It is well documented that the sort is "unstable", meaning two elements containing the same sort order value may or may not preserve the order from the original input. 有据可查的是,排序是“不稳定的”,这意味着包含相同排序顺序值的两个元素可能会或可能不会保留原始输入的顺序。 However, I need to know whether or not it is "deterministic", in the sense that any arbitrary input data will reproducibly return the same output data every time it is sorted. 但是,我需要知道它是否是“确定性的”,从某种意义上来说,任何任意输入数据每次进行排序都将可复制地返回相同的输出数据。 Specifically, I know that quicksort can be implemented either deterministically or non-deterministically (if the pivot is chosen at random), but I'm not sure which implementation is used for .NET's introspective sort. 具体来说,我知道快速排序可以确定性地或非确定性地实现(如果随机选择枢轴),但是我不确定.NET的自省式排序使用哪种实现。

From my testing, it seems to be deterministic, as I have not seen any particular set of data return differently between runs, but obviously you can't prove something doesn't exist simply because you haven't seen it :-/ 从我的测试来看,这似乎是确定性的,因为我没有看到任何特定的数据集在两次运行之间会有所不同,但是显然您不能仅仅因为未曾看到过就证明不存在某些东西:-/

I suppose I plan on looking at the code to help try to figure out whether or not introspective sort is deterministic, but I was hoping that someone here knows offhand and can save me the effort ;) 我想我打算看一下代码以帮助尝试找出内省性排序是否是确定性的,但是我希望这里的人能立即了解并可以省下我的工作;)

Thanks! 谢谢! Ryan 瑞安

Unfortunately, someone basically relied on it being deterministic in 4.0, and now that we have upgraded we are finding discrepancies. 不幸的是,有人基本上依靠它在4.0中具有确定性,而现在我们已经升级了,所以我们发现了差异。 What we'd like to do is compile against 4.0 and resort the same sets to get back what we were getting, we just want to make sure that when we do that, we can rely on the sorts being the same as they previously were. 我们想要做的是针对4.0编译并使用相同的集合来返回所获得的内容,我们只想确保当我们这样做时,我们可以依靠与以前相同的排序。 In other words, is the quicksort implementation Array.Sort in 4.0 deterministic? 换句话说,4.0中的快速排序实现Array.Sort是否具有确定性?

So you want to find out for a particular version whether Array.Sort is deterministic. 因此,您想了解特定版本的 Array.Sort是否具有确定性。 That is a much easier problem. 这是一个容易得多的问题。

Decompile the .NET BCL code (or look at the reference source). 反编译.NET BCL代码(或查看参考源)。 If the implementation is using only deterministic operations (ie not using Random ) the result is also deterministic. 如果实现仅使用确定性操作(即不使用Random ),则结果也是确定性的。

Last time I looked the sorting code fit onto just a few pages. 上一次,我发现排序代码仅适用于几页。 You'll quickly sift through it. 您将快速筛选它。 My guess: It will obviously be deterministic (if your comparer is as well!). 我的猜测:这显然将是确定性的(如果您的比较器也是如此!)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM