Package squidpony.squidmath
Class TimSort<T>
java.lang.Object
squidpony.squidmath.TimSort<T>
public class TimSort<T> extends Object
A stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when running on partially sorted
arrays, while offering performance comparable to a traditional mergesort when run on random arrays. Like all proper mergesorts,
this sort is stable and runs O(n log n) time (worst case). In the worst case, this sort requires temporary storage space for
n/2 object references; in the best case, it requires only a small constant amount of space.
Most users won't ever need this class directly; its API is meant for implementors of ordered data structures like
This implementation was adapted from Tim Peters's list sort for Python, which is described in detail here:
http://svn.python.org/projects/python/trunk/Objects/listsort.txt
Tim's C code may be found here:
http://svn.python.org/projects/python/trunk/Objects/listobject.c
The underlying techniques are described in this paper (and may have even earlier origins):
"Optimistic Sorting and Information Theoretic Complexity" Peter McIlroy SODA (Fourth Annual ACM-SIAM Symposium on Discrete Algorithms), pp 467-474, Austin, Texas, 25-27 January 1993.
While the API to this class consists solely of static methods, it is (privately) instantiable; a TimSort instance holds the state of an ongoing sort, assuming the input array is large enough to warrant the full-blown TimSort. Small arrays are sorted in place, using a binary insertion sort.
Most users won't ever need this class directly; its API is meant for implementors of ordered data structures like
OrderedMap that allow sorting. In many cases those data structures expose methods like
OrderedSet.sort(Comparator) or OrderedMap.sortByValue(Comparator, int, int), and using those class'
methods is a much better idea than trying to use TimSort directly on a data structure that already allows sorting.
This implementation was adapted from Tim Peters's list sort for Python, which is described in detail here:
http://svn.python.org/projects/python/trunk/Objects/listsort.txt
Tim's C code may be found here:
http://svn.python.org/projects/python/trunk/Objects/listobject.c
The underlying techniques are described in this paper (and may have even earlier origins):
"Optimistic Sorting and Information Theoretic Complexity" Peter McIlroy SODA (Fourth Annual ACM-SIAM Symposium on Discrete Algorithms), pp 467-474, Austin, Texas, 25-27 January 1993.
While the API to this class consists solely of static methods, it is (privately) instantiable; a TimSort instance holds the state of an ongoing sort, assuming the input array is large enough to warrant the full-blown TimSort. Small arrays are sorted in place, using a binary insertion sort.
-
Method Summary
Modifier and Type Method Description static <T> voidsort(T[] a, IntVLA order, int lo, int hi, Comparator<? super T> c)Modifiesorderby comparing items from indexloinclusive to indexhiexclusive in the arrayawith the Comparatorc; not likely to be used externally except by code that extends or re-implements SquidLib data structures.static <T> voidsort(T[] a, IntVLA order, Comparator<? super T> c)Modifiesorderby comparing items in the arrayawith the Comparatorc; not likely to be used externally except by code that extends or re-implements SquidLib data structures.
-
Method Details
-
sort
Modifiesorderby comparing items in the arrayawith the Comparatorc; not likely to be used externally except by code that extends or re-implements SquidLib data structures. Generally,orderis theOrderedMap.orderfield or some similar IntVLA used to keep order in an OrderedSet or the like; it will be modified in-place, but the other parameters will be unchanged.- Type Parameters:
T- the type of items inathatccan compare- Parameters:
a- an array of T, where items will be compared usingc; will not be modifiedorder- an IntVLA that stores indices inain the order they would be iterated through; will be modifiedc- a Comparator that can compare items ina
-
sort
Modifiesorderby comparing items from indexloinclusive to indexhiexclusive in the arrayawith the Comparatorc; not likely to be used externally except by code that extends or re-implements SquidLib data structures. Generally,orderis theOrderedMap.orderfield or some similar IntVLA used to keep order in an OrderedSet or the like; it will be modified in-place, but the other parameters will be unchanged.- Type Parameters:
T- the type of items inathatccan compare- Parameters:
a- an array of T, where items will be compared usingc; will not be modifiedorder- an IntVLA that stores indices inain the order they would be iterated through; will be modifiedlo- the inclusive start index to compare inaand change inorderhi- the exclusive end index to compare inaand change inorderc- a Comparator that can compare items ina
-