gpt4 book ai didi

java - 在 TimSort 方法中调用什么方法?

转载 作者:行者123 更新时间:2023-11-29 08:14:08 27 4
gpt4 key购买 nike

TimSort 是一种将在 Java 7 中默认用于排序的算法。

我找到了这个来源,但我不明白应该调用哪个方法,因为它们都是私有(private)的。有人能看懂吗?谢谢。

http://cr.openjdk.java.net/~martin/webrevs/openjdk7/timsort/raw_files/new/src/share/classes/java/util/TimSort.java

最佳答案

你什么都不叫。

它具有封装为 java.util 私有(private)的排序方法。当您调用 Arrays.sort() 函数或类似函数时,您可以让它调用它们。

评论清楚地表明了这一点:

/*
* The next two methods (which are package private and static) constitute
* the entire API of this class. Each of these methods obeys the contract
* of the public method with the same signature in java.util.Arrays.
*/

static <T> void sort(T[] a, Comparator<? super T> c) {
sort(a, 0, a.length, c);
}

static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c) {
...
}

根据我上次发表评论的时间来看,这只花了不到 15 分钟的时间:

结果:

C:\Documents and Settings\glowcoder\My Documents>java SortTest
Time for default: 4094ms
Time for timsort: 3813ms

C:\Documents and Settings\glowcoder\My Documents>

关于java - 在 TimSort 方法中调用什么方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5863509/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com