Class IntArrays
- java.lang.Object
-
- org.codelibs.jhighlight.fastutil.ints.IntArrays
-
public class IntArrays extends Object
A class providing static methods and objects that do useful things with type-specific arrays.In particular, the
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
org.codelibs.jhighlight.fastutil.io.BinIO
andorg.codelibs.jhighlight.fastutil.io.TextIO
contain several methods make it possible to load and save arrays of primitive types as sequences of elements inDataInput
format (i.e., not as objects) or as sequences of lines of text.- See Also:
Arrays
-
-
Field Summary
Fields Modifier and Type Field Description static int[]
EMPTY_ARRAY
A static, final, empty array.static Hash.Strategy<int[]>
HASH_STRATEGY
A type-specific content-based hash strategy for arrays.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static int
binarySearch(int[] a, int key)
Searches an array for the specified value using the binary search algorithm.static int
binarySearch(int[] a, int from, int to, int key)
Searches a range of the specified array for the specified value using the binary search algorithm.static int
binarySearch(int[] a, int from, int to, int key, IntComparator c)
Searches a range of the specified array for the specified value using the binary search algorithm and a specified comparator.static int
binarySearch(int[] a, int key, IntComparator c)
Searches an array for the specified value using the binary search algorithm and a specified comparator.static int[]
copy(int[] array)
Returns a copy of an array.static int[]
copy(int[] array, int offset, int length)
Returns a copy of a portion of an array.static int[]
ensureCapacity(int[] array, int length)
Ensures that an array can contain the given number of entries.static int[]
ensureCapacity(int[] array, int length, int preserve)
Ensures that an array can contain the given number of entries, preserving just a part of the array.static void
ensureFromTo(int[] a, int from, int to)
Ensures that a range given by its first (inclusive) and last (exclusive) elements fits an array.static void
ensureOffsetLength(int[] a, int offset, int length)
Ensures that a range given by an offset and a length fits an array.static boolean
equals(int[] a1, int[] a2)
Deprecated.Please use the correspondingArrays
method, which is intrinsified in recent JVMs.static void
fill(int[] array, int value)
Fills the given array with the given value.static void
fill(int[] array, int from, int to, int value)
Fills a portion of the given array with the given value.static int[]
grow(int[] array, int length)
Grows the given array to the maximum between the given length and the current length multiplied by two, provided that the given length is larger than the current length.static int[]
grow(int[] array, int length, int preserve)
Grows the given array to the maximum between the given length and the current length multiplied by two, provided that the given length is larger than the current length, preserving just a part of the array.static void
mergeSort(int[] a)
Sorts an array according to the natural ascending order using mergesort.static void
mergeSort(int[] a, int from, int to)
Sorts the specified range of elements according to the natural ascending order using mergesort.static void
mergeSort(int[] a, int from, int to, int[] supp)
Sorts the specified range of elements according to the natural ascending order using mergesort, using a given pre-filled support array.static void
mergeSort(int[] a, int from, int to, IntComparator comp)
Sorts the specified range of elements according to the order induced by the specified comparator using mergesort.static void
mergeSort(int[] a, int from, int to, IntComparator comp, int[] 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 void
mergeSort(int[] a, IntComparator comp)
Sorts an array according to the order induced by the specified comparator using mergesort.static void
quickSort(int[] x)
Sorts an array according to the natural ascending order using quicksort.static void
quickSort(int[] x, int from, int to)
Sorts the specified range of elements according to the natural ascending order using quicksort.static void
quickSort(int[] x, int from, int to, IntComparator comp)
Sorts the specified range of elements according to the order induced by the specified comparator using quicksort.static void
quickSort(int[] x, IntComparator comp)
Sorts an array according to the order induced by the specified comparator using quicksort.static void
radixSort(int[] a)
Sorts the specified array using radix sort.static void
radixSort(int[][] a)
Sorts the specified array of arrays lexicographically using radix sort.static void
radixSort(int[][] a, int from, int to)
Sorts the specified array of arrays lexicographically using radix sort.static void
radixSort(int[] a, int[] b)
Sorts the specified pair of arrays lexicographically using radix sort.static void
radixSort(int[] a, int[] b, int from, int to)
Sorts the specified pair of arrays lexicographically using radix sort.static void
radixSort(int[] a, int from, int to)
Sorts the specified array using radix sort.static void
radixSortIndirect(int[] perm, int[] a, boolean stable)
Sorts the specified array using indirect radix sort.static void
radixSortIndirect(int[] perm, int[] a, int[] b, boolean stable)
Sorts the specified pair of arrays lexicographically using indirect radix sort.static void
radixSortIndirect(int[] perm, int[] a, int[] b, int from, int to, boolean stable)
Sorts the specified pair of arrays lexicographically using indirect radix sort.static void
radixSortIndirect(int[] perm, int[] a, int from, int to, boolean stable)
Sorts the specified array using indirect radix sort.static int[]
reverse(int[] a)
Reverses the order of the elements in the specified array.static int[]
reverse(int[] a, int from, int to)
Reverses the order of the elements in the specified array fragment.static int[]
setLength(int[] array, int length)
Sets the length of the given array.static int[]
shuffle(int[] a, int from, int to, Random random)
Shuffles the specified array fragment using the specified pseudorandom number generator.static int[]
shuffle(int[] a, Random random)
Shuffles the specified array using the specified pseudorandom number generator.static int[]
trim(int[] array, int length)
Trims the given array to the given length.
-
-
-
Field Detail
-
EMPTY_ARRAY
public static final int[] EMPTY_ARRAY
A static, final, empty array.
-
HASH_STRATEGY
public static final Hash.Strategy<int[]> 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
null
correctly, and it is serializable.
-
-
Method Detail
-
ensureCapacity
public static int[] ensureCapacity(int[] 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 containslength
entries or more; otherwise, an array withlength
entries whose firstarray.length
entries are the same as those ofarray
.
-
ensureCapacity
public static int[] ensureCapacity(int[] 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 containlength
entries or more; otherwise, an array withlength
entries whose firstpreserve
entries are the same as those ofarray
.
-
grow
public static int[] grow(int[] array, int length)
Grows the given array to the maximum between the given length and the current length multiplied by two, 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 containlength
entries; otherwise, an array with max(length
,array.length
/φ) entries whose firstarray.length
entries are the same as those ofarray
.
-
grow
public static int[] grow(int[] array, int length, int preserve)
Grows the given array to the maximum between the given length and the current length multiplied by two, 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 containlength
entries; otherwise, an array with max(length
,array.length
/φ) entries whose firstpreserve
entries are the same as those ofarray
.
-
trim
public static int[] trim(int[] 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 containslength
entries or less; otherwise, an array withlength
entries whose entries are the same as the firstlength
entries ofarray
.
-
setLength
public static int[] setLength(int[] 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 exactlylength
entries; otherwise, if it contains more thanlength
entries, an array withlength
entries whose entries are the same as the firstlength
entries ofarray
; otherwise, an array withlength
entries whose firstarray.length
entries are the same as those ofarray
.
-
copy
public static int[] copy(int[] 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
length
elements ofarray
starting atoffset
.
-
copy
public static int[] copy(int[] array)
Returns a copy of an array.- Parameters:
array
- an array.- Returns:
- a copy of
array
.
-
fill
public static void fill(int[] array, int value)
Fills the given array with the given value.This method uses a backward loop. It is significantly faster than the corresponding method in
Arrays
.- Parameters:
array
- an array.value
- the new value for all elements of the array.
-
fill
public static void fill(int[] array, int from, int to, int value)
Fills a portion of the given array with the given value.If possible (i.e.,
from
is 0) this method uses a backward loop. In this case, it is significantly faster than the corresponding method inArrays
.- 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(int[] a1, int[] a2)
Deprecated.Please use the correspondingArrays
method, 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(int[] 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
- iffrom
is greater thanto
.ArrayIndexOutOfBoundsException
- iffrom
orto
are greater than the array length or negative.
-
ensureOffsetLength
public static void ensureOffsetLength(int[] 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
- iflength
is negative.ArrayIndexOutOfBoundsException
- ifoffset
is negative oroffset
+length
is greater than the array length.
-
quickSort
public static void quickSort(int[] x, int from, int to, IntComparator 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(int[] x, IntComparator 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.
-
quickSort
public static void quickSort(int[] 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(int[] 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.
-
mergeSort
public static void mergeSort(int[] a, int from, int to, int[] 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 leastto
elements, and whose entries are identical to those ofa
in the specified range.
-
mergeSort
public static void mergeSort(int[] 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
a
will 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(int[] 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
a
will be allocated by this method.- Parameters:
a
- the array to be sorted.
-
mergeSort
public static void mergeSort(int[] a, int from, int to, IntComparator comp, int[] 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 leastto
elements, and whose entries are identical to those ofa
in the specified range.
-
mergeSort
public static void mergeSort(int[] a, int from, int to, IntComparator 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
a
will 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(int[] a, IntComparator 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
a
will be allocated by this method.- Parameters:
a
- the array to be sorted.comp
- the comparator to determine the sorting order.
-
binarySearch
public static int binarySearch(int[] a, int from, int to, int key)
Searches a range of the specified array for the specified value using the binary search algorithm. The range must be sorted prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.- Parameters:
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.- Returns:
- index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 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.
- See Also:
Arrays
-
binarySearch
public static int binarySearch(int[] a, int key)
Searches an array for the specified value using the binary search algorithm. The range must be sorted prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.- Parameters:
a
- the array to be searched.key
- the value to be searched for.- Returns:
- index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 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.
- See Also:
Arrays
-
binarySearch
public static int binarySearch(int[] a, int from, int to, int key, IntComparator c)
Searches a range of the specified array for the specified value using the binary search algorithm and a specified comparator. The range must be sorted following the comparator prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.- Parameters:
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.- Returns:
- index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 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.
- See Also:
Arrays
-
binarySearch
public static int binarySearch(int[] a, int key, IntComparator c)
Searches an array for the specified value using the binary search algorithm and a specified comparator. The range must be sorted following the comparator prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.- Parameters:
a
- the array to be searched.key
- the value to be searched for.c
- a comparator.- Returns:
- index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 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.
- See Also:
Arrays
-
radixSort
public static void radixSort(int[] a)
Sorts the specified array using radix sort.The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This implementation is significantly faster than quicksort already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the array to be sorted.
- Parameters:
a
- the array to be sorted.
-
radixSort
public static void radixSort(int[] a, int from, int to)
Sorts the specified array using radix sort.The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This implementation is significantly faster than quicksort already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the array to be sorted.
- 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.
-
radixSortIndirect
public static void radixSortIndirect(int[] perm, int[] a, boolean stable)
Sorts the specified array using indirect radix sort.The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
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 thata[ perm[ i ] ] <= a[ perm[ i + 1 ] ]
.This implementation is significantly faster than quicksort (unstable) or mergesort (stable) already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the array to be sorted, and, in the stable case, a further support array as large as
perm
(note that the stable version is slightly faster).- Parameters:
perm
- a permutation array indexinga
.a
- the array to be sorted.stable
- whether the sorting algorithm should be stable.
-
radixSortIndirect
public static void radixSortIndirect(int[] perm, int[] a, int from, int to, boolean stable)
Sorts the specified array using indirect radix sort.The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
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 thata[ perm[ i ] ] <= a[ perm[ i + 1 ] ]
.This implementation is significantly faster than quicksort (unstable) or mergesort (stable) already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the array to be sorted, and, in the stable case, a further support array as large as
perm
(note that the stable version is slightly faster).- Parameters:
perm
- a permutation array indexinga
.a
- the array to be sorted.from
- the index of the first element ofperm
(inclusive) to be permuted.to
- the index of the last element ofperm
(exclusive) to be permuted.stable
- whether the sorting algorithm should be stable.
-
radixSort
public static void radixSort(int[] a, int[] b)
Sorts the specified pair of arrays lexicographically using radix sort.The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
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
a[ i ] < a[ i + 1 ]
ora[ i ] == a[ i + 1 ]
andb[ i ] <= b[ i + 1 ]
.This implementation is significantly faster than quicksort already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the arrays to be sorted.
- Parameters:
a
- the first array to be sorted.b
- the second array to be sorted.
-
radixSort
public static void radixSort(int[] a, int[] b, int from, int to)
Sorts the specified pair of arrays lexicographically using radix sort.The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
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
a[ i ] < a[ i + 1 ]
ora[ i ] == a[ i + 1 ]
andb[ i ] <= b[ i + 1 ]
.This implementation is significantly faster than quicksort already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the arrays to be sorted.
- Parameters:
a
- the first array to be sorted.b
- 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.
-
radixSortIndirect
public static void radixSortIndirect(int[] perm, int[] a, int[] b, boolean stable)
Sorts the specified pair of arrays lexicographically using indirect radix sort.The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
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 thata[ perm[ i ] ] <= a[ perm[ i + 1 ] ]
.This implementation is significantly faster than quicksort (unstable) or mergesort (stable) already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the array to be sorted, and, in the stable case, a further support array as large as
perm
(note that the stable version is slightly faster).- Parameters:
perm
- a permutation array indexinga
.a
- the array to be sorted.b
- the second array to be sorted.stable
- whether the sorting algorithm should be stable.
-
radixSortIndirect
public static void radixSortIndirect(int[] perm, int[] a, int[] b, int from, int to, boolean stable)
Sorts the specified pair of arrays lexicographically using indirect radix sort.The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
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 thata[ perm[ i ] ] <= a[ perm[ i + 1 ] ]
.This implementation is significantly faster than quicksort (unstable) or mergesort (stable) already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the array to be sorted, and, in the stable case, a further support array as large as
perm
(note that the stable version is slightly faster).- Parameters:
perm
- a permutation array indexinga
.a
- the array to be sorted.b
- the second array to be sorted.from
- the index of the first element ofperm
(inclusive) to be permuted.to
- the index of the last element ofperm
(exclusive) to be permuted.stable
- whether the sorting algorithm should be stable.
-
radixSort
public static void radixSort(int[][] a)
Sorts the specified array of arrays lexicographically using radix sort.The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This method implements a lexicographical sorting of the provided arrays. Tuples of elements in the same position will be considered a single key, and permuted accordingly.
This implementation is significantly faster than quicksort already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the arrays to be sorted.
- Parameters:
a
- an array containing arrays of equal length to be sorted lexicographically in parallel.
-
radixSort
public static void radixSort(int[][] a, int from, int to)
Sorts the specified array of arrays lexicographically using radix sort.The sorting algorithm is a tuned radix sort adapted from Peter M. McIlroy, Keith Bostic and M. Douglas McIlroy, “Engineering radix sort”, Computing Systems, 6(1), pages 5−27 (1993), and further improved using the digit-oracle idea described by Juha Kärkkäinen and Tommi Rantala in “Engineering radix sort for strings”, String Processing and Information Retrieval, 15th International Symposium, volume 5280 of Lecture Notes in Computer Science, pages 3−14, Springer (2008).
This method implements a lexicographical sorting of the provided arrays. Tuples of elements in the same position will be considered a single key, and permuted accordingly.
This implementation is significantly faster than quicksort already at small sizes (say, more than 10000 elements), but it can only sort in ascending order. It will allocate a support array of bytes with the same number of elements as the arrays to be sorted.
- Parameters:
a
- an array containing arrays of equal length to be sorted lexicographically in parallel.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.
-
shuffle
public static int[] shuffle(int[] 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 (please use a XorShift* generator).- Returns:
a
.
-
shuffle
public static int[] shuffle(int[] 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 (please use a XorShift* generator).- Returns:
a
.
-
reverse
public static int[] reverse(int[] a)
Reverses the order of the elements in the specified array.- Parameters:
a
- the array to be reversed.- Returns:
a
.
-
reverse
public static int[] reverse(int[] 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
.
-
-