public class Arrays extends Object
In addition to commodity methods, this class contains Swapper-based implementations
of quicksort and of
a stable, in-place mergesort. These
generic sorting methods can be used to sort any kind of list, but they find their natural
usage, for instance, in sorting arrays in parallel.
Arrays| Modifier and Type | Method and Description |
|---|---|
static void |
ensureFromTo(int arrayLength,
int from,
int to)
Ensures that a range given by its first (inclusive) and last (exclusive) elements fits an array of given length.
|
static void |
ensureOffsetLength(int arrayLength,
int offset,
int length)
Ensures that a range given by an offset and a length fits an array of given length.
|
static void |
mergeSort(int from,
int to,
IntComparator c,
Swapper swapper)
Sorts the specified range of elements using the specified swapper and according to the order induced by the specified
comparator using mergesort.
|
static void |
quickSort(int from,
int to,
IntComparator comp,
Swapper swapper)
Sorts the specified range of elements using the specified swapper and according to the order induced by the specified
comparator using quicksort.
|
public static void ensureFromTo(int arrayLength,
int from,
int to)
This method may be used whenever an array range check is needed.
arrayLength - an array length.from - a start index (inclusive).to - an end index (inclusive).IllegalArgumentException - if from is greater than to.ArrayIndexOutOfBoundsException - if from or to are greater than arrayLength or negative.public static void ensureOffsetLength(int arrayLength,
int offset,
int length)
This method may be used whenever an array range check is needed.
arrayLength - an array length.offset - a start index for the fragmentlength - a length (the number of elements in the fragment).IllegalArgumentException - if length is negative.ArrayIndexOutOfBoundsException - if offset is negative or offset+length is greater than arrayLength.public static void mergeSort(int from,
int to,
IntComparator c,
Swapper swapper)
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. The sorting algorithm is an in-place mergesort that is significantly slower than a standard mergesort, as its running time is O(n (log n)2), but it does not allocate additional memory; as a result, it can be used as a generic sorting algorithm.
from - the index of the first element (inclusive) to be sorted.to - the index of the last element (exclusive) to be sorted.c - the comparator to determine the order of the generic data (arguments are positions).swapper - an object that knows how to swap the elements at any two positions.public static void quickSort(int from,
int to,
IntComparator comp,
Swapper swapper)
The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.
from - the index of the first element (inclusive) to be sorted.to - the index of the last element (exclusive) to be sorted.comp - the comparator to determine the order of the generic data.swapper - an object that knows how to swap the elements at any two positions.