Java:Arrays.sort 快速排序和归并排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10251573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Arrays.sort quicksort and mergesort
提问by OckhamsRazor
Possible Duplicate:
Why java Arrays use two different sort algorithms for different types?
So I was reading the Arrays docon the various sort implementations. What I noticed was that some of the implementations used a tuned quicksort while others used a modified mergesort. Why the discrepancy?
所以我正在阅读有关各种排序实现的 Arrays文档。我注意到一些实现使用了调整后的快速排序,而其他实现使用了修改后的归并排序。为什么会出现差异?
Thanks!
谢谢!
回答by Eugene Retunsky
Quicksort is used for arrays of primitive types while mergesort for Object[] arrays.
快速排序用于原始类型的数组,而归并排序用于 Object[] 数组。
The main reason why mergesort is used for Objects that mergesort is stable - it does not reorder elements that are equal: http://en.wikipedia.org/wiki/Sorting_algorithm#Stability
合并排序用于对象的主要原因是合并排序是稳定的 - 它不会重新排序相等的元素:http: //en.wikipedia.org/wiki/Sorting_algorithm#Stability
For primitives the stability of the sort is meaningless, as you cannot distinguish two values that are equal. Hence, quicksort is used (except when sorting an array of Objects, for which mergesort is performed). Moreover, quicksort can be done in place, so there is no need to allocate another array.
对于原语,排序的稳定性毫无意义,因为您无法区分两个相等的值。因此,使用了快速排序(除非对执行归并排序的对象数组进行排序)。而且,quicksort可以就地完成,所以不需要分配另一个数组。