java - How does Collections.sort(...) work? -
to clear, trying find out how collections.sort(list, new mycomp()) method calls compare method in sequence.
i have linkedlist employees , personal number (k): numbers are: {1,2,3,4,5,6} compare(object o1, object o2) method in mycomparator gives number (which not relevant concern). how sort() call method compare? call parameters 1,2 then, 2,3 3,4 4,5 5,6? debug there's strange sequence jumps , compares 1,3.
what compare? pattern?
the specific comparisons made depend on algorithm, internally, collections.sort
method using sort elements. according javadoc collections.sort
:
the documentation polymorphic algorithms contained in class includes brief description of implementation. such descriptions should regarded implementation notes, rather parts of specification. implementors should feel free substitute other algorithms, long specification adhered to. (for example, algorithm used sort not have mergesort, have stable.)
in other words, java implementations free use whatever sorting algorithm they'd like, provided keeps equal elements in same relative order. means without more knowledge of specific java implementation, there's no way know comparisons going made. (if remember correctly, oracle version of java switched implementation of collections.sort
java 7 java 8, though may mistaken.)
that said, that's not bad thing. idea behind writing comparator tell sorting method "do whatever need sort things, , if ever need make comparison, here's way it." it's nice abstraction - how rank things, , magic black box of sorting goes , uses things order.
Comments
Post a Comment