Class BooleanArrays
- java.lang.Object
-
- it.unimi.dsi.fastutil.booleans.BooleanArrays
-
public final class BooleanArrays extends Object
A class providing static methods and objects that do useful things with type-specific arrays.In particular, the
forceCapacity(),ensureCapacity(),grow(),trim()andsetLength()methods allow to handle arrays much like array lists. This can be very useful when efficiency (or syntactic simplicity) reasons make array lists unsuitable.Note that
BinIOandTextIOcontain several methods make it possible to load and save arrays of primitive types as sequences of elements inDataInputformat (i.e., not as objects) or as sequences of lines of text.Sorting
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. Some algorithms also provide an explicit indirect sorting facility, which makes it possible to sort an array using the values in another array as comparator.
All comparison-based algorithm have an implementation based on a type-specific comparator.
As a general rule, sequential radix sort is significantly faster than quicksort or mergesort, in particular on random-looking data. In the parallel case, up to a few cores parallel radix sort is still the fastest, but at some point quicksort exploits parallelism better.
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
Arraysare 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.
- See Also:
Arrays
-
-
Field Summary
Fields Modifier and Type Field Description static boolean[]DEFAULT_EMPTY_ARRAYA static, final, empty array to be used as default array in allocations.static boolean[]EMPTY_ARRAYA static, final, empty array.static Hash.Strategy<boolean[]>HASH_STRATEGYA type-specific content-based hash strategy for arrays.
-
Method Summary
Modifier and Type Method Description static boolean[]copy(boolean[] array)Returns a copy of an array.static boolean[]copy(boolean[] array, int offset, int length)Returns a copy of a portion of an array.static boolean[]ensureCapacity(boolean[] array, int length)Ensures that an array can contain the given number of entries.static boolean[]ensureCapacity(boolean[] array, int length, int preserve)Ensures that an array can contain the given number of entries, preserving just a part of the array.static voidensureFromTo(boolean[] a, int from, int to)Ensures that a range given by its first (inclusive) and last (exclusive) elements fits an array.static voidensureOffsetLength(boolean[] a, int offset, int length)Ensures that a range given by an offset and a length fits an array.static voidensureSameLength(boolean[] a, boolean[] b)Ensures that two arrays are of the same length.static booleanequals(boolean[] a1, boolean[] a2)Deprecated.Please use the correspondingArraysmethod, which is intrinsified in recent JVMs.static voidfill(boolean[] array, boolean value)Deprecated.Please use the correspondingArraysmethod.static voidfill(boolean[] array, int from, int to, boolean value)Deprecated.Please use the correspondingArraysmethod.static boolean[]forceCapacity(boolean[] array, int length, int preserve)Forces an array to contain the given number of entries, preserving just a part of the array.static boolean[]grow(boolean[] 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 boolean[]grow(boolean[] 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 voidmergeSort(boolean[] a)Sorts an array according to the natural ascending order using mergesort.static voidmergeSort(boolean[] a, int from, int to)Sorts the specified range of elements according to the natural ascending order using mergesort.static voidmergeSort(boolean[] a, int from, int to, boolean[] supp)Sorts the specified range of elements according to the natural ascending order using mergesort, using a given pre-filled support array.static voidmergeSort(boolean[] a, int from, int to, BooleanComparator comp)Sorts the specified range of elements according to the order induced by the specified comparator using mergesort.static voidmergeSort(boolean[] a, int from, int to, BooleanComparator comp, boolean[] 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 voidmergeSort(boolean[] a, BooleanComparator comp)Sorts an array according to the order induced by the specified comparator using mergesort.static voidparallelQuickSort(boolean[] x)Sorts an array according to the natural ascending order using a parallel quicksort.static voidparallelQuickSort(boolean[] x, boolean[] y)Sorts two arrays according to the natural lexicographical ascending order using a parallel quicksort.static voidparallelQuickSort(boolean[] x, boolean[] 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 voidparallelQuickSort(boolean[] x, int from, int to)Sorts the specified range of elements according to the natural ascending order using a parallel quicksort.static voidparallelQuickSort(boolean[] x, int from, int to, BooleanComparator comp)Sorts the specified range of elements according to the order induced by the specified comparator using a parallel quicksort.static voidparallelQuickSort(boolean[] x, BooleanComparator comp)Sorts an array according to the order induced by the specified comparator using a parallel quicksort.static voidparallelQuickSortIndirect(int[] perm, boolean[] x)Sorts an array according to the natural ascending order using a parallel indirect quicksort.static voidparallelQuickSortIndirect(int[] perm, boolean[] x, int from, int to)Sorts the specified range of elements according to the natural ascending order using a parallel indirect quicksort.static voidquickSort(boolean[] x)Sorts an array according to the natural ascending order using quicksort.static voidquickSort(boolean[] x, boolean[] y)Sorts two arrays according to the natural lexicographical ascending order using quicksort.static voidquickSort(boolean[] x, boolean[] y, int from, int to)Sorts the specified range of elements of two arrays according to the natural lexicographical ascending order using quicksort.static voidquickSort(boolean[] x, int from, int to)Sorts the specified range of elements according to the natural ascending order using quicksort.static voidquickSort(boolean[] x, int from, int to, BooleanComparator comp)Sorts the specified range of elements according to the order induced by the specified comparator using quicksort.static voidquickSort(boolean[] x, BooleanComparator comp)Sorts an array according to the order induced by the specified comparator using quicksort.static voidquickSortIndirect(int[] perm, boolean[] x)Sorts an array according to the natural ascending order using indirect quicksort.static voidquickSortIndirect(int[] perm, boolean[] x, int from, int to)Sorts the specified range of elements according to the natural ascending order using indirect quicksort.static boolean[]reverse(boolean[] a)Reverses the order of the elements in the specified array.static boolean[]reverse(boolean[] a, int from, int to)Reverses the order of the elements in the specified array fragment.static boolean[]setLength(boolean[] array, int length)Sets the length of the given array.static boolean[]shuffle(boolean[] a, int from, int to, Random random)Shuffles the specified array fragment using the specified pseudorandom number generator.static boolean[]shuffle(boolean[] a, Random random)Shuffles the specified array using the specified pseudorandom number generator.static voidstabilize(int[] perm, boolean[] x)Stabilizes a permutation.static voidstabilize(int[] perm, boolean[] x, int from, int to)Stabilizes a permutation.static voidswap(boolean[] x, int a, int b)Swaps two elements of an anrray.static voidswap(boolean[] x, int a, int b, int n)Swaps two sequences of elements of an array.static boolean[]trim(boolean[] array, int length)Trims the given array to the given length.
-
-
-
Field Detail
-
EMPTY_ARRAY
public static final boolean[] EMPTY_ARRAY
A static, final, empty array.
-
DEFAULT_EMPTY_ARRAY
public static final boolean[] DEFAULT_EMPTY_ARRAY
A static, final, empty array to be used as default array in allocations. An object distinct fromEMPTY_ARRAYmakes it possible to have different behaviors depending on whether the user required an empty allocation, or we are just lazily delaying allocation.- See Also:
ArrayList
-
HASH_STRATEGY
public static final Hash.Strategy<boolean[]> HASH_STRATEGY
A type-specific content-based hash strategy for arrays.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
nullcorrectly, and it is serializable.
-
-
Method Detail
-
forceCapacity
public static boolean[] forceCapacity(boolean[] array, int length, int preserve)Forces an array to contain the given number of entries, preserving just a part of the array.- Parameters:
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.- Returns:
- an array with
lengthentries whose firstpreserveentries are the same as those ofarray.
-
ensureCapacity
public static boolean[] ensureCapacity(boolean[] array, int length)Ensures that an array can contain the given number of entries.If you cannot foresee whether this array will need again to be enlarged, you should probably use
grow()instead.- Parameters:
array- an array.length- the new minimum length for this array.- Returns:
array, if it containslengthentries or more; otherwise, an array withlengthentries whose firstarray.lengthentries are the same as those ofarray.
-
ensureCapacity
public static boolean[] ensureCapacity(boolean[] array, int length, int preserve)Ensures that an array can contain the given number of entries, preserving just a part of the array.- Parameters:
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.- Returns:
array, if it can containlengthentries or more; otherwise, an array withlengthentries whose firstpreserveentries are the same as those ofarray.
-
grow
public static boolean[] grow(boolean[] 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.If you want complete control on the array growth, you should probably use
ensureCapacity()instead.- Parameters:
array- an array.length- the new minimum length for this array.- Returns:
array, if it can containlengthentries; otherwise, an array with max(length,array.length/φ) entries whose firstarray.lengthentries are the same as those ofarray.
-
grow
public static boolean[] grow(boolean[] 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.If you want complete control on the array growth, you should probably use
ensureCapacity()instead.- Parameters:
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.- Returns:
array, if it can containlengthentries; otherwise, an array with max(length,array.length/φ) entries whose firstpreserveentries are the same as those ofarray.
-
trim
public static boolean[] trim(boolean[] array, int length)Trims the given array to the given length.- Parameters:
array- an array.length- the new maximum length for the array.- Returns:
array, if it containslengthentries or less; otherwise, an array withlengthentries whose entries are the same as the firstlengthentries ofarray.
-
setLength
public static boolean[] setLength(boolean[] array, int length)Sets the length of the given array.- Parameters:
array- an array.length- the new length for the array.- Returns:
array, if it contains exactlylengthentries; otherwise, if it contains more thanlengthentries, an array withlengthentries whose entries are the same as the firstlengthentries ofarray; otherwise, an array withlengthentries whose firstarray.lengthentries are the same as those ofarray.
-
copy
public static boolean[] copy(boolean[] array, int offset, int length)Returns a copy of a portion of an array.- Parameters:
array- an array.offset- the first element to copy.length- the number of elements to copy.- Returns:
- a new array containing
lengthelements ofarraystarting atoffset.
-
copy
public static boolean[] copy(boolean[] array)
Returns a copy of an array.- Parameters:
array- an array.- Returns:
- a copy of
array.
-
fill
@Deprecated public static void fill(boolean[] array, boolean value)
Deprecated.Please use the correspondingArraysmethod.Fills the given array with the given value.- Parameters:
array- an array.value- the new value for all elements of the array.
-
fill
@Deprecated public static void fill(boolean[] array, int from, int to, boolean value)
Deprecated.Please use the correspondingArraysmethod.Fills a portion of the given array with the given value.- Parameters:
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.
-
equals
@Deprecated public static boolean equals(boolean[] a1, boolean[] a2)
Deprecated.Please use the correspondingArraysmethod, which is intrinsified in recent JVMs.Returns true if the two arrays are elementwise equal.- Parameters:
a1- an array.a2- another array.- Returns:
- true if the two arrays are of the same length, and their elements are equal.
-
ensureFromTo
public static void ensureFromTo(boolean[] a, int from, int to)Ensures that a range given by its first (inclusive) and last (exclusive) elements fits an array.This method may be used whenever an array range check is needed.
- Parameters:
a- an array.from- a start index (inclusive).to- an end index (exclusive).- Throws:
IllegalArgumentException- iffromis greater thanto.ArrayIndexOutOfBoundsException- iffromortoare greater than the array length or negative.
-
ensureOffsetLength
public static void ensureOffsetLength(boolean[] a, int offset, int length)Ensures that a range given by an offset and a length fits an array.This method may be used whenever an array range check is needed.
- Parameters:
a- an array.offset- a start index.length- a length (the number of elements in the range).- Throws:
IllegalArgumentException- iflengthis negative.ArrayIndexOutOfBoundsException- ifoffsetis negative oroffset+lengthis greater than the array length.
-
ensureSameLength
public static void ensureSameLength(boolean[] a, boolean[] b)Ensures that two arrays are of the same length.- Parameters:
a- an array.b- another array.- Throws:
IllegalArgumentException- if the two argument arrays are not of the same length.
-
swap
public static void swap(boolean[] x, int a, int b)Swaps two elements of an anrray.- Parameters:
x- an array.a- a position inx.b- another position inx.
-
swap
public static void swap(boolean[] x, int a, int b, int n)Swaps two sequences of elements of an array.- Parameters:
x- an array.a- a position inx.b- another position inx.n- the number of elements to exchange starting ataandb.
-
quickSort
public static void quickSort(boolean[] x, int from, int to, BooleanComparator comp)Sorts the specified range of elements according to the order induced by the specified comparator using quicksort.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.- Parameters:
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.
-
quickSort
public static void quickSort(boolean[] x, BooleanComparator comp)Sorts an array according to the order induced by the specified comparator using quicksort.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.- Parameters:
x- the array to be sorted.comp- the comparator to determine the sorting order.
-
parallelQuickSort
public static void parallelQuickSort(boolean[] x, int from, int to, BooleanComparator comp)Sorts the specified range of elements according to the order induced by the specified comparator using a parallel quicksort.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
ForkJoinPoolexecutor service withRuntime.availableProcessors()parallel threads.- Parameters:
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.
-
parallelQuickSort
public static void parallelQuickSort(boolean[] x, BooleanComparator comp)Sorts an array according to the order induced by the specified comparator using a parallel quicksort.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
ForkJoinPoolexecutor service withRuntime.availableProcessors()parallel threads.- Parameters:
x- the array to be sorted.comp- the comparator to determine the sorting order.
-
quickSort
public static void quickSort(boolean[] x, int from, int to)Sorts the specified range of elements according to the natural ascending order using quicksort.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.- Parameters:
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.
-
quickSort
public static void quickSort(boolean[] x)
Sorts an array according to the natural ascending order using quicksort.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.- Parameters:
x- the array to be sorted.
-
parallelQuickSort
public static void parallelQuickSort(boolean[] x, int from, int to)Sorts the specified range of elements according to the natural ascending order using a parallel quicksort.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
ForkJoinPoolexecutor service withRuntime.availableProcessors()parallel threads.- Parameters:
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.
-
parallelQuickSort
public static void parallelQuickSort(boolean[] x)
Sorts an array according to the natural ascending order using a parallel quicksort.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
ForkJoinPoolexecutor service withRuntime.availableProcessors()parallel threads.- Parameters:
x- the array to be sorted.
-
quickSortIndirect
public static void quickSortIndirect(int[] perm, boolean[] x, int from, int to)Sorts the specified range of elements according to the natural ascending order using indirect quicksort.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 thatx[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.- Parameters:
perm- a permutation array indexingx.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.
-
quickSortIndirect
public static void quickSortIndirect(int[] perm, boolean[] x)Sorts an array according to the natural ascending order using indirect quicksort.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 thatx[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.- Parameters:
perm- a permutation array indexingx.x- the array to be sorted.
-
parallelQuickSortIndirect
public static void parallelQuickSortIndirect(int[] perm, boolean[] x, int from, int to)Sorts the specified range of elements according to the natural ascending order using a parallel indirect quicksort.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 thatx[perm[i]] ≤ x[perm[i + 1]].This implementation uses a
ForkJoinPoolexecutor service withRuntime.availableProcessors()parallel threads.- Parameters:
perm- a permutation array indexingx.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.
-
parallelQuickSortIndirect
public static void parallelQuickSortIndirect(int[] perm, boolean[] x)Sorts an array according to the natural ascending order using a parallel indirect quicksort.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 thatx[perm[i]] ≤ x[perm[i + 1]].This implementation uses a
ForkJoinPoolexecutor service withRuntime.availableProcessors()parallel threads.- Parameters:
perm- a permutation array indexingx.x- the array to be sorted.
-
stabilize
public static void stabilize(int[] perm, boolean[] x, int from, int to)Stabilizes a permutation.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 thatx[perm[i]] = x[perm[i + 1]]impliesperm[i] ≤ perm[i + 1].- Parameters:
perm- a permutation array indexingxso 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.
-
stabilize
public static void stabilize(int[] perm, boolean[] x)Stabilizes a permutation.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 thatx[perm[i]] = x[perm[i + 1]]impliesperm[i] ≤ perm[i + 1].- Parameters:
perm- a permutation array indexingxso that it is sorted.x- the sorted array to be stabilized.
-
quickSort
public static void quickSort(boolean[] x, boolean[] y, int from, int to)Sorts the specified range of elements of two arrays according to the natural lexicographical ascending order using quicksort.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]orx[i] == x[i + 1]andy[i] ≤ y[i + 1].- Parameters:
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.
-
quickSort
public static void quickSort(boolean[] x, boolean[] y)Sorts two arrays according to the natural lexicographical ascending order using quicksort.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]orx[i] == x[i + 1]andy[i] ≤ y[i + 1].- Parameters:
x- the first array to be sorted.y- the second array to be sorted.
-
parallelQuickSort
public static void parallelQuickSort(boolean[] x, boolean[] 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.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]orx[i] == x[i + 1]andy[i] ≤ y[i + 1].This implementation uses a
ForkJoinPoolexecutor service withRuntime.availableProcessors()parallel threads.- Parameters:
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.
-
parallelQuickSort
public static void parallelQuickSort(boolean[] x, boolean[] y)Sorts two arrays according to the natural lexicographical ascending order using a parallel quicksort.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]orx[i] == x[i + 1]andy[i] ≤ y[i + 1].This implementation uses a
ForkJoinPoolexecutor service withRuntime.availableProcessors()parallel threads.- Parameters:
x- the first array to be sorted.y- the second array to be sorted.
-
mergeSort
public static void mergeSort(boolean[] a, int from, int to, boolean[] supp)Sorts the specified range of elements according to the natural ascending order using mergesort, using a given pre-filled support array.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.
- Parameters:
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 leasttoelements, and whose entries are identical to those ofain the specified range.
-
mergeSort
public static void mergeSort(boolean[] a, int from, int to)Sorts the specified range of elements according to the natural ascending order using mergesort.This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. An array as large as
awill be allocated by this method.- Parameters:
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.
-
mergeSort
public static void mergeSort(boolean[] a)
Sorts an array according to the natural ascending order using mergesort.This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. An array as large as
awill be allocated by this method.- Parameters:
a- the array to be sorted.
-
mergeSort
public static void mergeSort(boolean[] a, int from, int to, BooleanComparator comp, boolean[] 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.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.
- Parameters:
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 leasttoelements, and whose entries are identical to those ofain the specified range.
-
mergeSort
public static void mergeSort(boolean[] a, int from, int to, BooleanComparator comp)Sorts the specified range of elements according to the order induced by the specified comparator using mergesort.This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. An array as large as
awill be allocated by this method.- Parameters:
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.
-
mergeSort
public static void mergeSort(boolean[] a, BooleanComparator comp)Sorts an array according to the order induced by the specified comparator using mergesort.This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. An array as large as
awill be allocated by this method.- Parameters:
a- the array to be sorted.comp- the comparator to determine the sorting order.
-
shuffle
public static boolean[] shuffle(boolean[] a, int from, int to, Random random)Shuffles the specified array fragment using the specified pseudorandom number generator.- Parameters:
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.- Returns:
a.
-
shuffle
public static boolean[] shuffle(boolean[] a, Random random)Shuffles the specified array using the specified pseudorandom number generator.- Parameters:
a- the array to be shuffled.random- a pseudorandom number generator.- Returns:
a.
-
reverse
public static boolean[] reverse(boolean[] a)
Reverses the order of the elements in the specified array.- Parameters:
a- the array to be reversed.- Returns:
a.
-
reverse
public static boolean[] reverse(boolean[] a, int from, int to)Reverses the order of the elements in the specified array fragment.- Parameters:
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.- Returns:
a.
-
-