public final class ObjectArrays
extends java.lang.Object
ensureCapacity(), grow(), trim()
and setLength() methods allow to handle arrays much like array lists.
This can be very useful when efficiency (or syntactic simplicity) reasons
make array lists unsuitable.
Warning: if your array is not of type Object[],
ensureCapacity(Object[],int,int) and grow(Object[],int,int)
will use reflection to preserve your array type. Reflection is significantly
slower than using new. This phenomenon is particularly evident
in the first growth phases of an array reallocated with doubling (or similar)
logic.
There are several sorting methods available. The main theme is that of letting you choose the sorting algorithm you prefer (i.e., trading stability of mergesort for no memory allocation in quicksort). Several algorithms provide a parallel version, that will use the number of cores available.
All comparison-based algorithm have an implementation based on a type-specific comparator.
If you are fine with not knowing exactly which algorithm will be run (in
particular, not knowing exactly whether a support array will be allocated),
the dual-pivot parallel sorts in Arrays are about 50%
faster than the classical single-pivot implementation used here.
In any case, if sorting time is important I suggest that you benchmark your sorting load with your data distribution and on your architecture.
Arrays| Modifier and Type | Field | Description |
|---|---|---|
static java.lang.Object[] |
DEFAULT_EMPTY_ARRAY |
A static, final, empty array to be used as default array in allocations.
|
static java.lang.Object[] |
EMPTY_ARRAY |
A static, final, empty array.
|
static Hash.Strategy |
HASH_STRATEGY |
A type-specific content-based hash strategy for arrays.
|
| Modifier and Type | Method | Description |
|---|---|---|
static <K> int |
binarySearch(K[] a,
int from,
int to,
K key) |
Searches a range of the specified array for the specified value using the
binary search algorithm.
|
static <K> int |
binarySearch(K[] a,
int from,
int to,
K key,
java.util.Comparator<K> c) |
Searches a range of the specified array for the specified value using the
binary search algorithm and a specified comparator.
|
static <K> int |
binarySearch(K[] a,
K key) |
Searches an array for the specified value using the binary search algorithm.
|
static <K> int |
binarySearch(K[] a,
K key,
java.util.Comparator<K> c) |
Searches an array for the specified value using the binary search algorithm
and a specified comparator.
|
static <K> K[] |
copy(K[] array) |
Returns a copy of an array.
|
static <K> K[] |
copy(K[] array,
int offset,
int length) |
Returns a copy of a portion of an array.
|
static <K> K[] |
ensureCapacity(K[] array,
int length) |
Ensures that an array can contain the given number of entries.
|
static <K> K[] |
ensureCapacity(K[] array,
int length,
int preserve) |
Ensures that an array can contain the given number of entries, preserving
just a part of the array.
|
static <K> void |
ensureFromTo(K[] a,
int from,
int to) |
Ensures that a range given by its first (inclusive) and last (exclusive)
elements fits an array.
|
static <K> void |
ensureOffsetLength(K[] a,
int offset,
int length) |
Ensures that a range given by an offset and a length fits an array.
|
static <K> void |
ensureSameLength(K[] a,
K[] b) |
Ensures that two arrays are of the same length.
|
static <K> boolean |
equals(K[] a1,
K[] a2) |
Deprecated.
Please use the corresponding
Arrays method,
which is intrinsified in recent JVMs. |
static <K> void |
fill(K[] array,
int from,
int to,
K value) |
Deprecated.
Please use the corresponding
Arrays method. |
static <K> void |
fill(K[] array,
K value) |
Deprecated.
Please use the corresponding
Arrays method. |
static <K> K[] |
forceCapacity(K[] array,
int length,
int preserve) |
Forces an array to contain the given number of entries, preserving just a
part of the array.
|
static <K> K[] |
grow(K[] array,
int length) |
Grows the given array to the maximum between the given length and the current
length increased by 50%, provided that the given length is larger than the
current length.
|
static <K> K[] |
grow(K[] array,
int length,
int preserve) |
Grows the given array to the maximum between the given length and the current
length increased by 50%, provided that the given length is larger than the
current length, preserving just a part of the array.
|
static <K> void |
mergeSort(K[] a) |
Sorts an array according to the natural ascending order using mergesort.
|
static <K> void |
mergeSort(K[] a,
int from,
int to) |
Sorts the specified range of elements according to the natural ascending
order using mergesort.
|
static <K> void |
mergeSort(K[] a,
int from,
int to,
java.util.Comparator<K> comp) |
Sorts the specified range of elements according to the order induced by the
specified comparator using mergesort.
|
static <K> void |
mergeSort(K[] a,
int from,
int to,
java.util.Comparator<K> comp,
K[] supp) |
Sorts the specified range of elements according to the order induced by the
specified comparator using mergesort, using a given pre-filled support array.
|
static <K> void |
mergeSort(K[] a,
int from,
int to,
K[] supp) |
Sorts the specified range of elements according to the natural ascending
order using mergesort, using a given pre-filled support array.
|
static <K> void |
mergeSort(K[] a,
java.util.Comparator<K> comp) |
Sorts an array according to the order induced by the specified comparator
using mergesort.
|
static <K> void |
parallelQuickSort(K[] x) |
Sorts an array according to the natural ascending order using a parallel
quicksort.
|
static <K> void |
parallelQuickSort(K[] x,
int from,
int to) |
Sorts the specified range of elements according to the natural ascending
order using a parallel quicksort.
|
static <K> void |
parallelQuickSort(K[] x,
int from,
int to,
java.util.Comparator<K> comp) |
Sorts the specified range of elements according to the order induced by the
specified comparator using a parallel quicksort.
|
static <K> void |
parallelQuickSort(K[] x,
java.util.Comparator<K> comp) |
Sorts an array according to the order induced by the specified comparator
using a parallel quicksort.
|
static <K> void |
parallelQuickSort(K[] x,
K[] y) |
Sorts two arrays according to the natural lexicographical ascending order
using a parallel quicksort.
|
static <K> void |
parallelQuickSort(K[] x,
K[] y,
int from,
int to) |
Sorts the specified range of elements of two arrays according to the natural
lexicographical ascending order using a parallel quicksort.
|
static <K> void |
parallelQuickSortIndirect(int[] perm,
K[] x) |
Sorts an array according to the natural ascending order using a parallel
indirect quicksort.
|
static <K> void |
parallelQuickSortIndirect(int[] perm,
K[] x,
int from,
int to) |
Sorts the specified range of elements according to the natural ascending
order using a parallel indirect quicksort.
|
static <K> void |
quickSort(K[] x) |
Sorts an array according to the natural ascending order using quicksort.
|
static <K> void |
quickSort(K[] x,
int from,
int to) |
Sorts the specified range of elements according to the natural ascending
order using quicksort.
|
static <K> void |
quickSort(K[] x,
int from,
int to,
java.util.Comparator<K> comp) |
Sorts the specified range of elements according to the order induced by the
specified comparator using quicksort.
|
static <K> void |
quickSort(K[] x,
java.util.Comparator<K> comp) |
Sorts an array according to the order induced by the specified comparator
using quicksort.
|
static <K> void |
quickSort(K[] x,
K[] y) |
Sorts two arrays according to the natural lexicographical ascending order
using quicksort.
|
static <K> void |
quickSort(K[] x,
K[] y,
int from,
int to) |
Sorts the specified range of elements of two arrays according to the natural
lexicographical ascending order using quicksort.
|
static <K> void |
quickSortIndirect(int[] perm,
K[] x) |
Sorts an array according to the natural ascending order using indirect
quicksort.
|
static <K> void |
quickSortIndirect(int[] perm,
K[] x,
int from,
int to) |
Sorts the specified range of elements according to the natural ascending
order using indirect quicksort.
|
static <K> K[] |
reverse(K[] a) |
Reverses the order of the elements in the specified array.
|
static <K> K[] |
reverse(K[] a,
int from,
int to) |
Reverses the order of the elements in the specified array fragment.
|
static <K> K[] |
setLength(K[] array,
int length) |
Sets the length of the given array.
|
static <K> K[] |
shuffle(K[] a,
int from,
int to,
java.util.Random random) |
Shuffles the specified array fragment using the specified pseudorandom number
generator.
|
static <K> K[] |
shuffle(K[] a,
java.util.Random random) |
Shuffles the specified array using the specified pseudorandom number
generator.
|
static <K> void |
stabilize(int[] perm,
K[] x) |
Stabilizes a permutation.
|
static <K> void |
stabilize(int[] perm,
K[] x,
int from,
int to) |
Stabilizes a permutation.
|
static <K> void |
swap(K[] x,
int a,
int b) |
Swaps two elements of an anrray.
|
static <K> void |
swap(K[] x,
int a,
int b,
int n) |
Swaps two sequences of elements of an array.
|
static <K> K[] |
trim(K[] array,
int length) |
Trims the given array to the given length.
|
public static final java.lang.Object[] EMPTY_ARRAY
public static final java.lang.Object[] DEFAULT_EMPTY_ARRAY
EMPTY_ARRAY makes it possible to have different
behaviors depending on whether the user required an empty allocation, or we
are just lazily delaying allocation.ArrayListpublic static final Hash.Strategy HASH_STRATEGY
This hash strategy may be used in custom hash collections whenever keys are
arrays, and they must be considered equal by content. This strategy will
handle null correctly, and it is serializable.
public static <K> K[] forceCapacity(K[] array,
int length,
int preserve)
array - an array.length - the new minimum length for this array.preserve - the number of elements of the array that must be preserved in case
a new allocation is necessary.length entries whose first preserve
entries are the same as those of array.public static <K> K[] ensureCapacity(K[] array,
int length)
If you cannot foresee whether this array will need again to be enlarged, you
should probably use grow() instead.
array - an array.length - the new minimum length for this array.array, if it contains length entries or more;
otherwise, an array with length entries whose first
array.length entries are the same as those of array.public static <K> K[] ensureCapacity(K[] array,
int length,
int preserve)
array - an array.length - the new minimum length for this array.preserve - the number of elements of the array that must be preserved in case
a new allocation is necessary.array, if it can contain length entries or more;
otherwise, an array with length entries whose first
preserve entries are the same as those of array.public static <K> K[] grow(K[] array,
int length)
If you want complete control on the array growth, you should probably use
ensureCapacity() instead.
array - an array.length - the new minimum length for this array.array, if it can contain length entries; otherwise,
an array with max(length,array.length/φ) entries
whose first array.length entries are the same as those of
array.public static <K> K[] grow(K[] array,
int length,
int preserve)
If you want complete control on the array growth, you should probably use
ensureCapacity() instead.
array - an array.length - the new minimum length for this array.preserve - the number of elements of the array that must be preserved in case
a new allocation is necessary.array, if it can contain length entries; otherwise,
an array with max(length,array.length/φ) entries
whose first preserve entries are the same as those of
array.public static <K> K[] trim(K[] array,
int length)
array - an array.length - the new maximum length for the array.array, if it contains length entries or less;
otherwise, an array with length entries whose entries are the
same as the first length entries of array.public static <K> K[] setLength(K[] array,
int length)
array - an array.length - the new length for the array.array, if it contains exactly length entries;
otherwise, if it contains more than length entries,
an array with length entries whose entries are the same as
the first length entries of array; otherwise, an
array with length entries whose first array.length
entries are the same as those of array.public static <K> K[] copy(K[] array,
int offset,
int length)
array - an array.offset - the first element to copy.length - the number of elements to copy.length elements of array
starting at offset.public static <K> K[] copy(K[] array)
array - an array.array.@Deprecated
public static <K> void fill(K[] array,
K value)
Arrays method.array - an array.value - the new value for all elements of the array.@Deprecated
public static <K> void fill(K[] array,
int from,
int to,
K value)
Arrays method.array - an array.from - the starting index of the portion to fill (inclusive).to - the end index of the portion to fill (exclusive).value - the new value for all elements of the specified portion of the
array.@Deprecated
public static <K> boolean equals(K[] a1,
K[] a2)
Arrays method,
which is intrinsified in recent JVMs.a1 - an array.a2 - another array.public static <K> void ensureFromTo(K[] a,
int from,
int to)
This method may be used whenever an array range check is needed.
a - an array.from - a start index (inclusive).to - an end index (exclusive).java.lang.IllegalArgumentException - if from is greater than to.java.lang.ArrayIndexOutOfBoundsException - if from or to are greater than the array length
or negative.public static <K> void ensureOffsetLength(K[] a,
int offset,
int length)
This method may be used whenever an array range check is needed.
a - an array.offset - a start index.length - a length (the number of elements in the range).java.lang.IllegalArgumentException - if length is negative.java.lang.ArrayIndexOutOfBoundsException - if offset is negative or offset+length is
greater than the array length.public static <K> void ensureSameLength(K[] a,
K[] b)
a - an array.b - another array.java.lang.IllegalArgumentException - if the two argument arrays are not of the same length.public static <K> void swap(K[] x,
int a,
int b)
x - an array.a - a position in x.b - another position in x.public static <K> void swap(K[] x,
int a,
int b,
int n)
x - an array.a - a position in x.b - another position in x.n - the number of elements to exchange starting at a and
b.public static <K> void quickSort(K[] x,
int from,
int to,
java.util.Comparator<K> comp)
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.
Note that this implementation does not allocate any object, contrarily to the
implementation used to sort primitive types in Arrays,
which switches to mergesort on large inputs.
x - the array to be sorted.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 sorting order.public static <K> void quickSort(K[] x,
java.util.Comparator<K> comp)
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.
Note that this implementation does not allocate any object, contrarily to the
implementation used to sort primitive types in Arrays,
which switches to mergesort on large inputs.
x - the array to be sorted.comp - the comparator to determine the sorting order.public static <K> void parallelQuickSort(K[] x,
int from,
int to,
java.util.Comparator<K> comp)
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.
This implementation uses a ForkJoinPool executor service with
Runtime.availableProcessors() parallel threads.
x - the array to be sorted.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 sorting order.public static <K> void parallelQuickSort(K[] x,
java.util.Comparator<K> comp)
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.
This implementation uses a ForkJoinPool executor service with
Runtime.availableProcessors() parallel threads.
x - the array to be sorted.comp - the comparator to determine the sorting order.public static <K> void quickSort(K[] x,
int from,
int to)
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.
Note that this implementation does not allocate any object, contrarily to the
implementation used to sort primitive types in Arrays,
which switches to mergesort on large inputs.
x - the array to be sorted.from - the index of the first element (inclusive) to be sorted.to - the index of the last element (exclusive) to be sorted.public static <K> void quickSort(K[] x)
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.
Note that this implementation does not allocate any object, contrarily to the
implementation used to sort primitive types in Arrays,
which switches to mergesort on large inputs.
x - the array to be sorted.public static <K> void parallelQuickSort(K[] x,
int from,
int to)
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.
This implementation uses a ForkJoinPool executor service with
Runtime.availableProcessors() parallel threads.
x - the array to be sorted.from - the index of the first element (inclusive) to be sorted.to - the index of the last element (exclusive) to be sorted.public static <K> void parallelQuickSort(K[] x)
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.
This implementation uses a ForkJoinPool executor service with
Runtime.availableProcessors() parallel threads.
x - the array to be sorted.public static <K> void quickSortIndirect(int[] perm,
K[] x,
int from,
int to)
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.
This method implement an indirect sort. The elements of perm
(which must be exactly the numbers in the interval [0..perm.length))
will be permuted so that x[perm[i]] ≤ x[perm[i + 1]].
Note that this implementation does not allocate any object, contrarily to the
implementation used to sort primitive types in Arrays,
which switches to mergesort on large inputs.
perm - a permutation array indexing x.x - the array to be sorted.from - the index of the first element (inclusive) to be sorted.to - the index of the last element (exclusive) to be sorted.public static <K> void quickSortIndirect(int[] perm,
K[] x)
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.
This method implement an indirect sort. The elements of perm
(which must be exactly the numbers in the interval [0..perm.length))
will be permuted so that x[perm[i]] ≤ x[perm[i + 1]].
Note that this implementation does not allocate any object, contrarily to the
implementation used to sort primitive types in Arrays,
which switches to mergesort on large inputs.
perm - a permutation array indexing x.x - the array to be sorted.public static <K> void parallelQuickSortIndirect(int[] perm,
K[] x,
int from,
int to)
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.
This method implement an indirect sort. The elements of perm
(which must be exactly the numbers in the interval [0..perm.length))
will be permuted so that x[perm[i]] ≤ x[perm[i + 1]].
This implementation uses a ForkJoinPool executor service with
Runtime.availableProcessors() parallel threads.
perm - a permutation array indexing x.x - the array to be sorted.from - the index of the first element (inclusive) to be sorted.to - the index of the last element (exclusive) to be sorted.public static <K> void parallelQuickSortIndirect(int[] perm,
K[] x)
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.
This method implement an indirect sort. The elements of perm
(which must be exactly the numbers in the interval [0..perm.length))
will be permuted so that x[perm[i]] ≤ x[perm[i + 1]].
This implementation uses a ForkJoinPool executor service with
Runtime.availableProcessors() parallel threads.
perm - a permutation array indexing x.x - the array to be sorted.public static <K> void stabilize(int[] perm,
K[] x,
int from,
int to)
This method can be used to stabilize the permutation generated by an indirect
sorting, assuming that initially the permutation array was in ascending order
(e.g., the identity, as usually happens). This method scans the permutation,
and for each non-singleton block of elements with the same associated values
in x, permutes them in ascending order. The resulting permutation
corresponds to a stable sort.
Usually combining an unstable indirect sort and this method is more efficient than using a stable sort, as most stable sort algorithms require a support array.
More precisely, assuming that x[perm[i]] ≤ x[perm[i + 1]], after
stabilization we will also have that x[perm[i]] = x[perm[i + 1]]
implies perm[i] ≤ perm[i + 1].
perm - a permutation array indexing x so that it is sorted.x - the sorted array to be stabilized.from - the index of the first element (inclusive) to be stabilized.to - the index of the last element (exclusive) to be stabilized.public static <K> void stabilize(int[] perm,
K[] x)
This method can be used to stabilize the permutation generated by an indirect
sorting, assuming that initially the permutation array was in ascending order
(e.g., the identity, as usually happens). This method scans the permutation,
and for each non-singleton block of elements with the same associated values
in x, permutes them in ascending order. The resulting permutation
corresponds to a stable sort.
Usually combining an unstable indirect sort and this method is more efficient than using a stable sort, as most stable sort algorithms require a support array.
More precisely, assuming that x[perm[i]] ≤ x[perm[i + 1]], after
stabilization we will also have that x[perm[i]] = x[perm[i + 1]]
implies perm[i] ≤ perm[i + 1].
perm - a permutation array indexing x so that it is sorted.x - the sorted array to be stabilized.public static <K> void quickSort(K[] x,
K[] y,
int from,
int to)
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.
This method implements a lexicographical sorting of the arguments.
Pairs of elements in the same position in the two provided arrays will be
considered a single key, and permuted accordingly. In the end, either
x[i] < x[i + 1] or x[i]
== x[i + 1] and y[i] ≤ y[i + 1].
x - the first array to be sorted.y - the second array to be sorted.from - the index of the first element (inclusive) to be sorted.to - the index of the last element (exclusive) to be sorted.public static <K> void quickSort(K[] x,
K[] y)
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.
This method implements a lexicographical sorting of the arguments.
Pairs of elements in the same position in the two provided arrays will be
considered a single key, and permuted accordingly. In the end, either
x[i] < x[i + 1] or x[i]
== x[i + 1] and y[i] ≤ y[i + 1].
x - the first array to be sorted.y - the second array to be sorted.public static <K> void parallelQuickSort(K[] x,
K[] y,
int from,
int to)
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.
This method implements a lexicographical sorting of the arguments.
Pairs of elements in the same position in the two provided arrays will be
considered a single key, and permuted accordingly. In the end, either
x[i] < x[i + 1] or x[i]
== x[i + 1] and y[i] ≤ y[i + 1].
This implementation uses a ForkJoinPool executor service with
Runtime.availableProcessors() parallel threads.
x - the first array to be sorted.y - the second array to be sorted.from - the index of the first element (inclusive) to be sorted.to - the index of the last element (exclusive) to be sorted.public static <K> void parallelQuickSort(K[] x,
K[] y)
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.
This method implements a lexicographical sorting of the arguments.
Pairs of elements in the same position in the two provided arrays will be
considered a single key, and permuted accordingly. In the end, either
x[i] < x[i + 1] or x[i]
== x[i + 1] and y[i] ≤ y[i + 1].
This implementation uses a ForkJoinPool executor service with
Runtime.availableProcessors() parallel threads.
x - the first array to be sorted.y - the second array to be sorted.public static <K> void mergeSort(K[] a,
int from,
int to,
K[] supp)
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. Moreover, no support arrays will be allocated.
a - the array to be sorted.from - the index of the first element (inclusive) to be sorted.to - the index of the last element (exclusive) to be sorted.supp - a support array containing at least to elements, and whose
entries are identical to those of a in the specified
range.public static <K> void mergeSort(K[] a,
int from,
int to)
This sort is guaranteed to be stable: equal elements will not be
reordered as a result of the sort. An array as large as a will be
allocated by this method.
a - the array to be sorted.from - the index of the first element (inclusive) to be sorted.to - the index of the last element (exclusive) to be sorted.public static <K> void mergeSort(K[] a)
This sort is guaranteed to be stable: equal elements will not be
reordered as a result of the sort. An array as large as a will be
allocated by this method.
a - the array to be sorted.public static <K> void mergeSort(K[] a,
int from,
int to,
java.util.Comparator<K> comp,
K[] supp)
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. Moreover, no support arrays will be allocated.
a - the array to be sorted.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 sorting order.supp - a support array containing at least to elements, and whose
entries are identical to those of a in the specified
range.public static <K> void mergeSort(K[] a,
int from,
int to,
java.util.Comparator<K> comp)
This sort is guaranteed to be stable: equal elements will not be
reordered as a result of the sort. An array as large as a will be
allocated by this method.
a - the array to be sorted.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 sorting order.public static <K> void mergeSort(K[] a,
java.util.Comparator<K> comp)
This sort is guaranteed to be stable: equal elements will not be
reordered as a result of the sort. An array as large as a will be
allocated by this method.
a - the array to be sorted.comp - the comparator to determine the sorting order.public static <K> int binarySearch(K[] a,
int from,
int to,
K key)
a - the array to be searched.from - the index of the first element (inclusive) to be searched.to - the index of the last element (exclusive) to be searched.key - the value to be searched for.(-(<i>insertion point</i>) - 1). The insertion point
is defined as the the point at which the value would be inserted into
the array: the index of the first element greater than the key, or
the length of the array, if all elements in the array are less than
the specified key. Note that this guarantees that the return value
will be ≥ 0 if and only if the key is found.Arrayspublic static <K> int binarySearch(K[] a,
K key)
a - the array to be searched.key - the value to be searched for.(-(<i>insertion point</i>) - 1). The insertion point
is defined as the the point at which the value would be inserted into
the array: the index of the first element greater than the key, or
the length of the array, if all elements in the array are less than
the specified key. Note that this guarantees that the return value
will be ≥ 0 if and only if the key is found.Arrayspublic static <K> int binarySearch(K[] a,
int from,
int to,
K key,
java.util.Comparator<K> c)
a - the array to be searched.from - the index of the first element (inclusive) to be searched.to - the index of the last element (exclusive) to be searched.key - the value to be searched for.c - a comparator.(-(<i>insertion point</i>) - 1). The insertion point
is defined as the the point at which the value would be inserted into
the array: the index of the first element greater than the key, or
the length of the array, if all elements in the array are less than
the specified key. Note that this guarantees that the return value
will be ≥ 0 if and only if the key is found.Arrayspublic static <K> int binarySearch(K[] a,
K key,
java.util.Comparator<K> c)
a - the array to be searched.key - the value to be searched for.c - a comparator.(-(<i>insertion point</i>) - 1). The insertion point
is defined as the the point at which the value would be inserted into
the array: the index of the first element greater than the key, or
the length of the array, if all elements in the array are less than
the specified key. Note that this guarantees that the return value
will be ≥ 0 if and only if the key is found.Arrayspublic static <K> K[] shuffle(K[] a,
int from,
int to,
java.util.Random random)
a - the array to be shuffled.from - the index of the first element (inclusive) to be shuffled.to - the index of the last element (exclusive) to be shuffled.random - a pseudorandom number generator.a.public static <K> K[] shuffle(K[] a,
java.util.Random random)
a - the array to be shuffled.random - a pseudorandom number generator.a.public static <K> K[] reverse(K[] a)
a - the array to be reversed.a.public static <K> K[] reverse(K[] a,
int from,
int to)
a - the array to be reversed.from - the index of the first element (inclusive) to be reversed.to - the index of the last element (exclusive) to be reversed.a.