The interview was asked, how is the sorting method in the collection class implemented? No answer comes up, so summarize as follows: you know what?
premise: in Eclipse for your own code you can press CTRL while clicking the name to jump into the appropriate source. However, Eclipse does not add Java source code directory by default, such as clicking on thread will prompt source not found, while in development it is necessary to know Java source code. The JDK defaults to the source Zip package (the Src.zip file in the JDK-mounted directory), which we can view in eclipse by adding the source code. in the prompt Source not found interface, click Attach source...->external File, select Src.zip in the JDK directory. (The JDK directory can be viewed in the system variable%java_home%.)
First, the code is as follows:
Import java.util.*;
public class Sort {
public static void Main (String args[]) {
List List = new ArrayList ();
List.add (123); List.add (321); List.add (87);
Collections.sort (list);
for (int i = 0;i <list.size (); i++) {
System.out.println (List.get (i));
}
}
}
Output:
87
123
321
Then, let's look at the Collections.sort () method and jump to the following code:
public class collections{
@SuppressWarnings ("Unchecked")
public static <t extends Comparable<? Super t>> void Sort (list<t> List) {
List.sort (NULL);
}
Then we click on the List.sort () method and jump to the following:
Public interface List<e> extends collection<e>{
@SuppressWarnings ({"Unchecked", "Rawtypes"})
default void sort (comparator< super e> c) {//JDK 1.8 new feature, interface can write method entity, add default before method.
Object[] A = This.toarray ();
Arrays.sort (A, (Comparator) c);
listiterator<e> i = This.listiterator ();
for (Object e:a) {
I.next ();
I.set ((e) e);
}
}
Then, the question arises, in the Collection.sort () method, List.sort (null) passed in null, but in the List.sort () function, the parameter is the default void sort (comparator<?). Super e> C), then CTRL click Comparator,
Then we see the Call of the Arrays.sort () method, into this arrays.sort () method,
public class arrays{
public static <T> void sort (t[] A, comparator<? super T> C) {
if (c = = null) {
Sort (a);
} else {
if (legacymergesort.userrequested)
Legacymergesort (A, c);
Else
Timsort.sort (A, 0, a.length, c, NULL, 0, 0);
}
}
Enter the Timesort.sort () method:
Class timesort{
static <T> void sort (t[] A, int lo, int hi, comparator<? super T> C, t[] work, int workbase, int worklen) {
Assert c! = null && A! = null && lo >= 0 && lo <= hi && hi <= a.length;
int nremaining = Hi-lo;
if (Nremaining < 2)
Return Arrays of size 0 and 1 is always sorted
If array is small, does a "mini-timsort" with no merges
if (Nremaining < Min_merge) {
int initrunlen = countrunandmakeascending (A, lo, Hi, c);
Binarysort (A, lo, hi, lo + initrunlen, c);
Return
}
From this we can see that its underlying invocation is implemented by the Binarysort () method.
Other Excellent blog references: (also on the Collections.sort () explanation)
1.http://trinea.iteye.com/blog/1248517
Java Basic Collection Class summary Collections.sort ()