Question

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

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top