简体   繁体   English

如何实现Collection.sort?

[英]How Collection.sort in is implemented?

I'm using Collection.sort(Array<objectX>, Comperator<objectX>) in java/Android. 我在Java / Android中使用Collection.sort(Array<objectX>, Comperator<objectX>)

In my comperator I compare two objects using their ID. 在我的编译器中,我使用它们的ID比较两个对象。

How can I know in which method this sort is made? 我怎么知道用哪种方法制作的? (quicksort, mergesort...)? (快速排序,合并排序...)?

Technically speaking which algorithm to use is not specified (in other words it's runtime library specific). 从技术上讲,没有指定要使用的算法(换句话说,它是特定于运行时库的)。

From the documentation : 文档中

For example, the algorithm used by sort does not have to be a mergesort, but it does have to be stable . 例如, sort使用的算法不必一定是mergesort,但是必须稳定

In practice, it typically chooses one sorting technique of of several based on the length of the list etc. In the standard implementation from Sun/Oracle it chooses between insertion sort (if the length is less than 7) and merge sort otherwise. 实际上,它通常根据列表的长度等选择一种排序技术。在Sun / Oracle的标准实现中,它在插入排序(如果长度小于7)和合并排序之间进行选择。

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

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