문제

TimSort is an algorithm that will be used by default in Java 7 for sorting.

I found this source, but I don't understand which method to call since all of them are private. Can anybody understand? Thank you.

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

도움이 되었습니까?

해결책

You don't call anything.

It has sort methods that are package private to java.util. You let it call them when you call the Arrays.sort() function or something like it.

This is made clear by the comment:

/*
 * 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) {
    ...
}

Judging by the time of my last comment, this took less than 15 minutes to do:

And the result:

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

C:\Documents and Settings\glowcoder\My Documents>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top