|
|||||||||
PREV CLASS NEXT CLASS | All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjhplot.math.SortUtils
public class SortUtils
Utility class providing some useful static sort methods. The sort routines
all return index permutations p such that data[p[0]],..,data[p[data.length-1]]
is in sorted order. The data array itself is not modified.
To actually rearrange the array elements, the inverse of p can be used to
permute the array, such that data[0],..,data[data.length-1] is in sorted
order. Use getIterator(p, data)
to iterate in sorted order.
A code example may show you what to do next:
String[] colors = { "red", "green", "blue" }; int[] p = SortUtils.sort(colors, new StringComparator()); // --> (colors[p[0]], colors[p[1]], colors[p[2]]) == ("blue","green","red") Iterator iter = SortUtils.getIterator(p, colors) // --> (iter.next(), iter.next(), iter.next()) == ("blue","green","red") SortUtils.permute(SortUtils.inverse(p), colors, true); // --> (colors[0], colors[1], colors[2]) == ("blue","green","red")Stable sorts (preserving order of equal elements) are supported. Sorting is done using quick-sort mith median of 3 (and insertion-sort for small ranges).
Constructor Summary | |
---|---|
SortUtils()
|
Method Summary | |
---|---|
static Iterator |
getIterator(int[] p,
List data)
Answer iterator, which iterates over specified data list according to the specified permutation, that is data.get(p[0]),..,data.get(p[data.length-1]) |
static Iterator |
getIterator(int[] p,
Object[] data)
Answer iterator, which iterates over specified data array according to the specified permutation, that is data[p[0]],..,data[p[data.length-1]] |
static int[] |
identity(int n)
Create identity permutation, that is {0, 1, ..., n} |
static int[] |
inverse(int[] p)
Compute inverse permutation |
static void |
main(String[] args)
Test method |
static Object[] |
permute(int[] p,
Object[] data,
boolean clone)
Rearrange the specified data according to the specified permutation. |
static int[] |
reverse(int n)
Create reverse permutation, that is {n-1, .... |
static void |
sort(int[] indices,
Object[] data,
Comparator comparator)
Do an unstable sort. |
static void |
sort(int[] indices,
Object[] data,
Comparator comparator,
boolean stable)
Do a sort on indices. |
static int[] |
sort(Object[] data,
Comparator comparator)
Do an unstable sort. |
static int[] |
sort(Object[] data,
Comparator comparator,
boolean stable)
Do a sort on indices. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public SortUtils()
Method Detail |
---|
public static int[] identity(int n)
{0, 1, ..., n}
public static int[] reverse(int n)
{n-1, .... 1, 0}
public static int[] inverse(int[] p)
public static Object[] permute(int[] p, Object[] data, boolean clone)
data_after[p[i]] == data_before[i]
.
data
- data to be permutedp
- the permutationclone
- if true, rearrange a clone instead of the original data;
public static Iterator getIterator(int[] p, Object[] data)
data[p[0]],..,data[p[data.length-1]]
public static Iterator getIterator(int[] p, List data)
data.get(p[0]),..,data.get(p[data.length-1])
public static void sort(int[] indices, Object[] data, Comparator comparator, boolean stable)
data
- data to be sortedcomparator
- comparator to usestable
- do a stable sort iff trueindices
- into data (any permutation of 0,..data.length-1).public static int[] sort(Object[] data, Comparator comparator, boolean stable)
data
- data to be sortedcomparator
- comparator to usestable
- do a stable sort iff true
public static int[] sort(Object[] data, Comparator comparator)
data
- data to be sortedcomparator
- comparator to use
public static void sort(int[] indices, Object[] data, Comparator comparator)
data
- data to be sortedindices
- into data (permutation of 0,..data.length-1).public static void main(String[] args)
|
|||||||||
PREV CLASS NEXT CLASS | All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |