Class ArraysUtils
- java.lang.Object
-
- cc.redberry.core.utils.ArraysUtils
-
public final class ArraysUtils extends java.lang.ObjectThis class contains additional methods for manipulating arrays (such as sorting and searching). For all quick sort methods the base code was taken from jdk6Arraysclass.- See Also:
Arrays
-
-
Field Summary
Fields Modifier and Type Field and Description static java.util.Comparator<java.lang.Object>HASH_COMPARATOR
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static int[]addAll(int[]... arrays)static int[]addAll(int[] array1, int... array2)static <T> T[]addAll(T[] array1, T... array2)This code is taken from Apache Commons Lang ArrayUtils.static Tensor[]addAll(Tensor[] array1, Tensor... array2)static voidarraycopy(IntArray source, int srcPos, int[] dest, int destPos, int length)static <T extends java.lang.Comparable<? super T>>
int[]bijection(T[] from, T[] to)Creates a bijective mapping between two arrays and returns the resulting bijection as array.static <T> int[]bijection(T[] from, T[] to, java.util.Comparator<? super T> comparator)This method is similar tobijection(Comparable[], Comparable[])}, but uses specifiedcomparator.static intbinarySearch(IntArray array, int key)Searches a range of the specified array of ints for the specified value using the binary search algorithm.static intbinarySearch(IntArrayList list, int key)Searches a range of the specified array of ints for the specified value using the binary search algorithm.static intbinarySearch1(int[] a, int key)This is the same method toArrays.binarySearch(int[], int).static intbinarySearch1(int[] a, int fromIndex, int toIndex, int key)This is the same method toArrays.binarySearch(int[], int, int, int).static int[]byte2int(byte[] a)static short[]byte2short(byte[] a)static intcommutativeHashCode(java.lang.Object[] objects)Returns xor of objects hashes viaHashFunctions.JenkinWang32shift(int)static intcommutativeHashCode(java.lang.Object[] objects, int from, int to)Returns xor of objects hashes spreaded byHashFunctions.JenkinWang32shift(int)static int[][]deepClone(int[][] input)static voidfill(IntArrayList list, int value)Assigns the specified int value to each element of the specified list of ints.static voidfill(IntArrayList list, int fromIndex, int toIndex, int value)Assigns the specified int value to each element of the specified range of the specified list.static int[]getSeriesFrom0(int size)static voidinsertionSort(int[] target, int[] coSort)Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes thecoSortints array in the same way as the target array.static voidinsertionSort(int[] target, int fromIndex, int toIndex, int[] coSort)Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes thecoSortints array in the same way as the target array.static voidinsertionSort(int[] target, int fromIndex, int toIndex, long[] coSort)Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes thecoSortints array in the same way as the target array.static voidinsertionSort(int[] target, long[] coSort)Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes thecoSortlongs array in the same way as the specified target array.static <T extends java.lang.Comparable<T>>
voidinsertionSort(T[] target, int fromIndex, int toIndex, java.lang.Object[] coSort)Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements using insertion sort algorithm and simultaneously permutes thecoSortobjects array in the same way then specified target array.static <T extends java.lang.Comparable<T>>
voidinsertionSort(T[] target, java.lang.Object[] coSort)Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements using insertion sort algorithm and simultaneously permutes thecoSortobjects array in the same way then specified target array.static byte[]int2byte(int[] a)static short[]int2short(int[] a)static intmax(int[] array)static voidquickSort(int[] target, int[] coSort)Sorts the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortints array in the same way as the target array.static voidquickSort(int[] target, int[] cosort, IntComparator comparator)Sorts the specified target array of ints according toIntComparatorand simultaneously permutes thecoSortObjects array in the same way as the target array.static voidquickSort(int[] target, IntComparator comparator)Sorts the specified range of the specified target array of ints into order specified byIntComparatorusing quicksort.static voidquickSort(int[] target, int fromIndex, int toIndex, int[] coSort)Sorts the specified range of the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortints array in the same way as the target array.static voidquickSort(int[] target, int fromIndex, int toIndex, int[] cosort, IntComparator comparator)Sorts the specified range of the specified target array of ints according toIntComparatorand simultaneously permutes thecoSortObjects array in the same way as the target array.static voidquickSort(int[] target, int fromIndex, int toIndex, IntComparator comparator)Sorts the specified range of the specified target array of ints into order specified byIntComparatorusing quicksort.static voidquickSort(int[] target, int fromIndex, int toIndex, long[] coSort)Sorts the specified range of the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortlongs array in the same way as the target array.static voidquickSort(int[] target, int fromIndex, int toIndex, java.lang.Object[] coSort)Sorts the specified range of the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortObjects array in the same way as the target array.static voidquickSort(int[] target, long[] coSort)Sorts the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortlongs array in the same way as the target array.static voidquickSort(int[] target, java.lang.Object[] coSort)Sorts the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortObjects array in the same way as the target array.static voidquickSort(short[] target, int[] coSort)Sorts the specified target array of shorts into ascending numerical order and simultaneously permutes thecoSortints array in the same way as the target array.static voidquickSort(short[] target, int fromIndex, int toIndex, int[] coSort)Sorts the specified range of the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortints array in the same way as the target array.static <T extends java.lang.Comparable<T>>
voidquickSort(T[] target, int[] coSort)Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements and simultaneously permutes thecoSortobjects array in the same way then specified target array.static <T extends java.lang.Comparable<T>>
voidquickSort(T[] target, int fromIndex, int toIndex, int[] coSort)Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements and simultaneously permutes thecoSortobjects array in the same way then specified target array.static <T extends java.lang.Comparable<T>>
voidquickSort(T[] target, int fromIndex, int toIndex, java.lang.Object[] coSort)Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements and simultaneously permutes thecoSortobjects array in the same way then specified target array.static <T extends java.lang.Comparable<T>>
voidquickSort(T[] target, java.lang.Object[] coSort)Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements and simultaneously permutes thecoSortobjects array in the same way then specified target array.static voidquickSort1(int[] target, int fromIndex, int length, int[] coSort)This method is the same asquickSort(int[], int, int, int[]), but without range checking and toIndex -> length (see params).static voidquickSort1(int[] target, int fromIndex, int length, IntComparator comparator)Sorts the specified range of the specified target array of ints into order specified byIntComparatorusing quicksort.static voidquickSort1(int[] target, int fromIndex, int length, long[] coSort)This method is the same as), but without range checking.static voidquickSort1(int[] target, int fromIndex, int length, java.lang.Object[] coSort)This method is the same as), but without range checking.static voidquickSort1(short[] target, int fromIndex, int length, int[] coSort)This method is the same asquickSort(int[], int, int, int[]), but without range checking and toIndex -> length (see params).static <T extends java.lang.Comparable<T>>
voidquickSort1(T[] target, int fromIndex, int length, int[] coSort)This method is the same asquickSort(Comparable[], int, int, Object[]), but without range checking.static <T extends java.lang.Comparable<T>>
voidquickSort1(T[] target, int fromIndex, int length, java.lang.Object[] coSort)This method is the same asquickSort(Comparable[], int, int, Object[]), but without range checking.static int[]quickSortP(int[] target)Sorts the specified array and returns the resulting permutationstatic int[]quickSortP(short[] target)static int[]remove(int[] array, int[] positions)Removes elements at specifiedpositionsin specifiedarray.static <T> T[]remove(T[] array, int i)static <T> T[]remove(T[] array, int[] positions)Removes elements at specifiedpositionsin specifiedarray.static Tensor[]remove(Tensor[] array, int i)static Tensor[]remove(Tensor[] array, int[] positions)Removes elements at specifiedpositionsin specifiedarray.static <T> T[]select(T[] array, int[] positions)Selects elements from specifiedarrayat specifiedpositions.static int[]short2int(short[] a)static voidstableSort(int[] target, int[] cosort)Sorts the specified array of ints into ascending order using stable sort algorithm and simultaneously permutes thecoSortints array in the same way as the target array.static intsum(int[] array)static voidswap(int[] x, int a, int b)Swaps x[a] with x[b].static voidswap(java.lang.Object[] x, int a, int b)Swaps x[a] with x[b].static voidtimSort(int[] target, int[] coSort)Sorts the specified array of ints into ascending order using TimSort algorithm and simultaneously permutes thecoSortints array in the same way as the target array.static int[]toArray(java.util.Set<java.lang.Integer> set)ConvertsSet<Integer>toint[]static java.lang.StringtoString(int[] a, ToStringConverter<java.lang.Integer> format)static <T> java.lang.StringtoString(T[] a, ToStringConverter<T> format)
-
-
-
Field Detail
-
HASH_COMPARATOR
public static final java.util.Comparator<java.lang.Object> HASH_COMPARATOR
-
-
Method Detail
-
arraycopy
public static void arraycopy(IntArray source, int srcPos, int[] dest, int destPos, int length)
-
short2int
public static int[] short2int(short[] a)
-
int2short
public static short[] int2short(int[] a)
-
byte2int
public static int[] byte2int(byte[] a)
-
byte2short
public static short[] byte2short(byte[] a)
-
int2byte
public static byte[] int2byte(int[] a)
-
max
public static int max(int[] array)
-
getSeriesFrom0
public static int[] getSeriesFrom0(int size)
-
deepClone
public static int[][] deepClone(int[][] input)
-
sum
public static int sum(int[] array)
-
bijection
public static <T> int[] bijection(T[] from, T[] to, java.util.Comparator<? super T> comparator)This method is similar tobijection(Comparable[], Comparable[])}, but uses specifiedcomparator.- Parameters:
from- from arrayto- to arraycomparator- comparator- Returns:
- a bijective mapping from
from-array toto-array andnullif no mapping exist
-
bijection
public static <T extends java.lang.Comparable<? super T>> int[] bijection(T[] from, T[] to)Creates a bijective mapping between two arrays and returns the resulting bijection as array. Method returns null, if no mapping found.Example:
Integer from[] = {1,2,1,4}; Integer to[] = {2,4,1,1}; int[] bijection = bijection(from,to);The resulting bijection will be
[2, 0, 3, 1]- Parameters:
from- from arrayto- to array- Returns:
- a bijective mapping from
from-array toto-array andnullif no mapping exist
-
addAll
public static int[] addAll(int[] array1, int... array2)
-
addAll
public static int[] addAll(int[]... arrays)
-
remove
public static <T> T[] remove(T[] array, int i)
-
addAll
@SafeVarargs public static <T> T[] addAll(T[] array1, T... array2)This code is taken from Apache Commons Lang ArrayUtils.Adds all the elements of the given arrays into a new array.
The new array contains all of the element of
array1followed by all of the elementsarray2. When an array is returned, it is always a new array.ArrayUtils.addAll(null, null) = null ArrayUtils.addAll(array1, null) = cloned copy of array1 ArrayUtils.addAll(null, array2) = cloned copy of array2 ArrayUtils.addAll([], []) = [] ArrayUtils.addAll([null], [null]) = [null, null] ArrayUtils.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
- Type Parameters:
T- the component type of the array- Parameters:
array1- the first array whose elements are added to the new array, may benullarray2- the second array whose elements are added to the new array, may benull- Returns:
- The new array,
nullif both arrays arenull. The type of the new array is the type of the first array, unless the first array is null, in which case the type is the same as the second array. - Throws:
java.lang.IllegalArgumentException- if the array types are incompatible- Since:
- 2.1
-
remove
public static <T> T[] remove(T[] array, int[] positions)Removes elements at specifiedpositionsin specifiedarray. This method preserve the relative order of elements in specifiedarray.- Type Parameters:
T- generic type- Parameters:
array- array of elementspositions- positions of elements that should be removed- Returns:
- new array with removed elements at specified positions
- Throws:
java.lang.ArrayIndexOutOfBoundsException- if some position larger then array length
-
remove
public static Tensor[] remove(Tensor[] array, int[] positions)
Removes elements at specifiedpositionsin specifiedarray. This method preserve the relative order of elements in specifiedarray.- Parameters:
array- array of elementspositions- positions of elements that should be removed- Returns:
- new array with removed elements at specified positions
- Throws:
java.lang.ArrayIndexOutOfBoundsException- if some position larger then array length
-
remove
public static int[] remove(int[] array, int[] positions)Removes elements at specifiedpositionsin specifiedarray. This method preserve the relative order of elements in specifiedarray.- Parameters:
array- array of elementspositions- positions of elements that should be removed- Returns:
- new array with removed elements at specified positions
- Throws:
java.lang.ArrayIndexOutOfBoundsException- if some position larger then array length
-
select
public static <T> T[] select(T[] array, int[] positions)Selects elements from specifiedarrayat specifiedpositions. The resulting array preserves the relative order of elements in specifiedarray.- Type Parameters:
T- generic type- Parameters:
array- array of elementspositions- of elements that should be picked out- Returns:
- the array of elements that picked out from specified positions in specified array
-
toArray
public static int[] toArray(java.util.Set<java.lang.Integer> set)
ConvertsSet<Integer>toint[]- Parameters:
set- aSetofInteger- Returns:
int[]
-
fill
public static void fill(IntArrayList list, int fromIndex, int toIndex, int value)
Assigns the specified int value to each element of the specified range of the specified list. The range to be filled extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be filled is empty.)- Parameters:
list- the list to be filledfromIndex- the index of the first element (inclusive) to be filled with the specified valuetoIndex- the index of the last element (exclusive) to be filled with the specified valuevalue- the value to be stored in all elements of the array- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.ArrayIndexOutOfBoundsException- if fromIndex < 0 or toIndex > a.length
-
fill
public static void fill(IntArrayList list, int value)
Assigns the specified int value to each element of the specified list of ints.- Parameters:
list- the array to be filledvalue- the value to be stored in all elements of the array
-
binarySearch
public static int binarySearch(IntArrayList list, int key)
Searches a range of the specified array of ints for the specified value using the binary search algorithm. The range must be sorted (as by theIntArrayList.sort()method) 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:
list- the list to be searchedkey- the value to be searched for- Returns:
- index of the search key, if it is contained in the array within the specified range; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range 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(int[], int)
-
binarySearch
public static int binarySearch(IntArray array, int key)
Searches a range of the specified array of ints for the specified value using the binary search algorithm. The range must be sorted (as by theIntArrayList.sort()method) 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:
array- the list to be searchedkey- the value to be searched for- Returns:
- index of the search key, if it is contained in the array within the specified range; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range 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(int[], int)
-
binarySearch1
public static int binarySearch1(int[] a, int key)This is the same method toArrays.binarySearch(int[], int). The differs is in the returned value. If key not found, this method returns the position of the first element, witch is closest to key (i.e. if Arrays.binarySearch returns-low-1, this method returnslow).- Parameters:
a- the array to be searchedkey- the value to be searched for- Returns:
- index of the search key, if it is contained in the array; otherwise, insertion point. The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key.
-
binarySearch1
public static int binarySearch1(int[] a, int fromIndex, int toIndex, int key)This is the same method toArrays.binarySearch(int[], int, int, int). The differs is in the returned value. If key not found, this method returns the position of the first element, witch is closest to key (i.e. if Arrays.binarySearch returns-low-1, this method returnslow).- Parameters:
a- the array to be searchedkey- the value to be searched forfromIndex- the index of the first element (inclusive) to be searchedtoIndex- the index of the last element (exclusive) to be searched- Returns:
- index of the search key, if it is contained in the array; otherwise, insertion point. The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or toIndex if all elements in the array are less than the specified key.
-
commutativeHashCode
public static int commutativeHashCode(java.lang.Object[] objects)
Returns xor of objects hashes viaHashFunctions.JenkinWang32shift(int)- Parameters:
objects- array- Returns:
- commutative hash
-
commutativeHashCode
public static int commutativeHashCode(java.lang.Object[] objects, int from, int to)Returns xor of objects hashes spreaded byHashFunctions.JenkinWang32shift(int)- Parameters:
objects- array- Returns:
- commutative hash
-
insertionSort
public static void insertionSort(int[] target, int[] coSort)Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes thecoSortints array in the same way as the target array. This sort guarantee O(n^2) performance in the worst case and O(n) in the best case (nearly sorted input).This sort is the best choice for small arrays with elements number < 100.
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort; adaptive: performance adapts to the initial order of elements and in-place: requires constant amount of additional space.
- Parameters:
target- the array to be sortedcoSort- the array which will be permuted in the same way as the target array during sorting procedure- Throws:
java.lang.IllegalArgumentException- if coSort length less then target length.
-
insertionSort
public static void insertionSort(int[] target, int fromIndex, int toIndex, int[] coSort)Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes thecoSortints array in the same way as the target array. This sort guarantee O(n^2) performance in the worst case and O(n) in the best case (nearly sorted input). The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.)This sort is the best choice for small arrays with elements number < 100.
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort; adaptive: performance adapts to the initial order of elements and in-place: requires constant amount of additional space.
- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedtoIndex- the index of the last element (exclusive) to be sortedcoSort- the array which will be permuted in the same way as the target array, during sorting procedure- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.ArrayIndexOutOfBoundsException- if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
-
insertionSort
public static void insertionSort(int[] target, long[] coSort)Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes thecoSortlongs array in the same way as the specified target array. This sort guarantee O(n^2) performance in the worst case and O(n) in the best case (nearly sorted input).This sort is the best choice for small arrays with elements number < 100.
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort; adaptive: performance adapts to the initial order of elements and in-place: requires constant amount of additional space.
- Parameters:
target- the array to be sortedcoSort- the array which will be permuted in the same way as the target array during sorting procedure- Throws:
java.lang.IllegalArgumentException- if coSort length less then target length.
-
insertionSort
public static void insertionSort(int[] target, int fromIndex, int toIndex, long[] coSort)Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes thecoSortints array in the same way as the target array. This sort guarantee O(n^2) performance in the worst case and O(n) in the best case (nearly sorted input). The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.)This sort is the best choice for small arrays with elements number < 100.
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort; adaptive: performance adapts to the initial order of elements and in-place: requires constant amount of additional space.
- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedtoIndex- the index of the last element (exclusive) to be sortedcoSort- the array which will be permuted in the same way as the specified target array, during sorting procedure- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.ArrayIndexOutOfBoundsException- if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
-
insertionSort
public static <T extends java.lang.Comparable<T>> void insertionSort(T[] target, java.lang.Object[] coSort)Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements using insertion sort algorithm and simultaneously permutes thecoSortobjects array in the same way then specified target array. This sort guarantee O(n^2) performance in the worst case and O(n) in the best case (nearly sorted input).This sort is the best choice for small arrays with elements number < 100.
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort; adaptive: performance adapts to the initial order of elements and in-place: requires constant amount of additional space.
- Parameters:
target- the array to be sortedcoSort- the array which will be permuted in the same way as the target array during sorting procedure- Throws:
java.lang.IllegalArgumentException- if coSort length less then target length.
-
insertionSort
public static <T extends java.lang.Comparable<T>> void insertionSort(T[] target, int fromIndex, int toIndex, java.lang.Object[] coSort)Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements using insertion sort algorithm and simultaneously permutes thecoSortobjects array in the same way then specified target array. This sort guarantee O(n^2) performance in the worst case and O(n) in the best case (nearly sorted input). The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.)This sort is the best choice for small arrays with elements number < 100.
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort; adaptive: performance adapts to the initial order of elements and in-place: requires constant amount of additional space.
- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedtoIndex- the index of the last element (exclusive) to be sortedcoSort- the array which will be permuted in the same way as the target array, during sorting procedure- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.ArrayIndexOutOfBoundsException- if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
-
timSort
public static void timSort(int[] target, int[] coSort)Sorts the specified array of ints into ascending order using TimSort algorithm and simultaneously permutes thecoSortints array in the same way as the target array.NOTE: using of this method is very good for large arrays with more then 100 elements, in other case using of insertion sort is highly recommended.
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
The code was taken from
Arrays.sort(java.lang.Object[])and adapted for integers. For more information look there.- Parameters:
target- the array to be sortedcoSort- the array which will be permuted in the same way as the target array during sorting procedure- Throws:
java.lang.ClassCastException- if the array contains elements that are not mutually comparable (for example, strings and integers)- See Also:
Arrays.sort(java.lang.Object[])
-
stableSort
public static void stableSort(int[] target, int[] cosort)Sorts the specified array of ints into ascending order using stable sort algorithm and simultaneously permutes thecoSortints array in the same way as the target array. If length of specified array is less than 100 - insertion sort algorithm performed, otherwise - TimSort.This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
- Parameters:
target- the array to be sortedcosort- the array which will be permuted in the same way as the target array during sorting procedure- Throws:
java.lang.ClassCastException- if the array contains elements that are not mutually comparable (for example, strings and integers)java.lang.IllegalArgumentException- if coSort length less then target length.java.lang.IllegalArgumentException- if target == coSort (as references).- See Also:
insertionSort(int[], int[]),timSort(int[], int[])
-
quickSortP
public static int[] quickSortP(int[] target)
Sorts the specified array and returns the resulting permutation- Parameters:
target- int array- Returns:
- sorting permutation
-
quickSort
public static void quickSort(int[] target, int[] coSort)Sorts the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortints array in the same way as the target array. The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: remember this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.NOTE: The method throws
IllegalArgumentExceptioniftarget == coSort, because in this case no sorting will be perfomed.- Parameters:
target- the array to be sortedcoSort- the array which will be permuted in the same way as the target array during sorting procedure- Throws:
java.lang.IllegalArgumentException- if coSort length less then target length.java.lang.IllegalArgumentException- if target == coSort (as references).
-
quickSort
public static void quickSort(int[] target, int fromIndex, int toIndex, int[] coSort)Sorts the specified range of the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortints array in the same way as the target array. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.)
The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: remember this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.NOTE: The method throws
IllegalArgumentExceptioniftarget == coSort, because in this case no sorting will be perfomed.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedtoIndex- the index of the last element (exclusive) to be sortedcoSort- the array which will be permuted in the same way as the target array, during sorting procedure- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.ArrayIndexOutOfBoundsException- if fromIndex < 0 or toIndex > target.length or toIndex > coSort.lengthjava.lang.IllegalArgumentException- if target == coSort (as references).
-
quickSort1
public static void quickSort1(int[] target, int fromIndex, int length, int[] coSort)This method is the same asquickSort(int[], int, int, int[]), but without range checking and toIndex -> length (see params). ThrowsIllegalArgumentExceptioniftarget == coSort, because in this case no sorting will be perfomed .NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedlength- the length of the sorting subarray.coSort- the array which will be permuted in the same way as the target array, during sorting procedure- Throws:
java.lang.IllegalArgumentException- if target == coSort (as references).
-
swap
public static void swap(int[] x, int a, int b)Swaps x[a] with x[b].
-
quickSort
public static void quickSort(int[] target, long[] coSort)Sorts the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortlongs array in the same way as the target array. The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedcoSort- the array which will be permuted in the same way as the target array during sorting procedure- Throws:
java.lang.IllegalArgumentException- if coSort length less then target length.
-
quickSort
public static void quickSort(int[] target, int fromIndex, int toIndex, long[] coSort)Sorts the specified range of the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortlongs array in the same way as the target array. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.)
The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be performed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedtoIndex- the index of the last element (exclusive) to be sortedcoSort- the array which will be permuted in the same way as the target array, during sorting procedure- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.ArrayIndexOutOfBoundsException- if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
-
quickSort1
public static void quickSort1(int[] target, int fromIndex, int length, long[] coSort)This method is the same as), but without range checking.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be performed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedlength- the length of the sorting subarray.coSort- the array which will be permuted in the same way as the target array, during sorting procedure
-
quickSort
public static <T extends java.lang.Comparable<T>> void quickSort(T[] target, java.lang.Object[] coSort)Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements and simultaneously permutes thecoSortobjects array in the same way then specified target array. The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedcoSort- the array which will be permuted in the same way as the target array during sorting procedure- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.IllegalArgumentException- if coSort length less then target length.java.lang.IllegalArgumentException- if target == coSort (as references).
-
quickSort
public static <T extends java.lang.Comparable<T>> void quickSort(T[] target, int fromIndex, int toIndex, java.lang.Object[] coSort)Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements and simultaneously permutes thecoSortobjects array in the same way then specified target array. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.)
The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedtoIndex- the index of the last element (exclusive) to be sortedcoSort- the array which will be permuted in the same way as the target array, during sorting procedure- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.ArrayIndexOutOfBoundsException- if fromIndex < 0 or toIndex > target.length or toIndex > coSort.lengthjava.lang.IllegalArgumentException- if target == coSort (as references).
-
quickSort1
public static <T extends java.lang.Comparable<T>> void quickSort1(T[] target, int fromIndex, int length, java.lang.Object[] coSort)This method is the same asquickSort(Comparable[], int, int, Object[]), but without range checking.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedlength- the length of the sorting subarray.coSort- the array which will be permuted in the same way as the target array, during sorting procedure- Throws:
java.lang.IllegalArgumentException- if target == coSort (as references).
-
quickSort
public static <T extends java.lang.Comparable<T>> void quickSort(T[] target, int[] coSort)Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements and simultaneously permutes thecoSortobjects array in the same way then specified target array. The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedcoSort- the array which will be permuted in the same way as the target array during sorting procedure- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.IllegalArgumentException- if coSort length less then target length.java.lang.IllegalArgumentException- if target == coSort (as references).
-
quickSort
public static <T extends java.lang.Comparable<T>> void quickSort(T[] target, int fromIndex, int toIndex, int[] coSort)Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements and simultaneously permutes thecoSortobjects array in the same way then specified target array. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.)
The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedtoIndex- the index of the last element (exclusive) to be sortedcoSort- the array which will be permuted in the same way as the target array, during sorting procedure- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.ArrayIndexOutOfBoundsException- if fromIndex < 0 or toIndex > target.length or toIndex > coSort.lengthjava.lang.IllegalArgumentException- if target == coSort (as references).
-
quickSort1
public static <T extends java.lang.Comparable<T>> void quickSort1(T[] target, int fromIndex, int length, int[] coSort)This method is the same asquickSort(Comparable[], int, int, Object[]), but without range checking.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedlength- the length of the sorting subarray.coSort- the array which will be permuted in the same way as the target array, during sorting procedure- Throws:
java.lang.IllegalArgumentException- if target == coSort (as references).
-
swap
public static void swap(java.lang.Object[] x, int a, int b)Swaps x[a] with x[b].
-
quickSort
public static void quickSort(int[] target, java.lang.Object[] coSort)Sorts the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortObjects array in the same way as the target array. The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedcoSort- the array which will be permuted in the same way as the target array during sorting procedure- Throws:
java.lang.IllegalArgumentException- if coSort length less then target length.
-
quickSort
public static void quickSort(int[] target, int fromIndex, int toIndex, java.lang.Object[] coSort)Sorts the specified range of the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortObjects array in the same way as the target array. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.)
The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedtoIndex- the index of the last element (exclusive) to be sortedcoSort- the array which will be permuted in the same way as the target array, during sorting procedure- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.ArrayIndexOutOfBoundsException- if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
-
quickSort1
public static void quickSort1(int[] target, int fromIndex, int length, java.lang.Object[] coSort)This method is the same as), but without range checking.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedlength- the length of the sorting subarray.coSort- the array which will be permuted in the same way as the target array, during sorting procedure
-
quickSortP
public static int[] quickSortP(short[] target)
-
quickSort
public static void quickSort(short[] target, int fromIndex, int toIndex, int[] coSort)Sorts the specified range of the specified target array of ints into ascending numerical order and simultaneously permutes thecoSortints array in the same way as the target array. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.)
The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: remember this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.NOTE: The method throws
IllegalArgumentExceptioniftarget == coSort, because in this case no sorting will be perfomed.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedtoIndex- the index of the last element (exclusive) to be sortedcoSort- the array which will be permuted in the same way as the target array, during sorting procedure- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.ArrayIndexOutOfBoundsException- if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
-
quickSort1
public static void quickSort1(short[] target, int fromIndex, int length, int[] coSort)This method is the same asquickSort(int[], int, int, int[]), but without range checking and toIndex -> length (see params). ThrowsIllegalArgumentExceptioniftarget == coSort, because in this case no sorting will be perfomed .NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedlength- the length of the sorting subarray.coSort- the array which will be permuted in the same way as the target array, during sorting procedure
-
quickSort
public static void quickSort(short[] target, int[] coSort)Sorts the specified target array of shorts into ascending numerical order and simultaneously permutes thecoSortints array in the same way as the target array. The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: remember this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.NOTE: The method throws
IllegalArgumentExceptioniftarget == coSort, because in this case no sorting will be perfomed.- Parameters:
target- the array to be sortedcoSort- the array which will be permuted in the same way as the target array during sorting procedure- Throws:
java.lang.IllegalArgumentException- if coSort length less then target length.
-
quickSort
public static void quickSort(int[] target, IntComparator comparator)Sorts the specified range of the specified target array of ints into order specified byIntComparatorusing quicksort.- Parameters:
target- the array to be sortedcomparator- custom comparator- Throws:
java.lang.IllegalArgumentException- if coSort length less then target length.
-
quickSort
public static void quickSort(int[] target, int fromIndex, int toIndex, IntComparator comparator)Sorts the specified range of the specified target array of ints into order specified byIntComparatorusing quicksort.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedtoIndex- the index of the last element (exclusive) to be sortedcomparator- comparator- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.ArrayIndexOutOfBoundsException- if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
-
quickSort1
public static void quickSort1(int[] target, int fromIndex, int length, IntComparator comparator)Sorts the specified range of the specified target array of ints into order specified byIntComparatorusing quicksort.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedlength- the length of the sorting subarray.comparator- comparator
-
quickSort
public static void quickSort(int[] target, int[] cosort, IntComparator comparator)Sorts the specified target array of ints according toIntComparatorand simultaneously permutes thecoSortObjects array in the same way as the target array. The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedcomparator- custom comparator- Throws:
java.lang.IllegalArgumentException- if coSort length less then target length.
-
quickSort
public static void quickSort(int[] target, int fromIndex, int toIndex, int[] cosort, IntComparator comparator)Sorts the specified range of the specified target array of ints according toIntComparatorand simultaneously permutes thecoSortObjects array in the same way as the target array. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.)
The code was taken from the jdk6 Arrays class. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.NOTE: this is unstable sort algorithm, so additional combinatorics of the
coSortarray can be perfomed. Use this method only if you are sure, in what you are doing. If not - use stable sort methods like an insertion sort or Tim sort.- Parameters:
target- the array to be sortedfromIndex- the index of the first element (inclusive) to be sortedtoIndex- the index of the last element (exclusive) to be sortedcomparator- comparator- Throws:
java.lang.IllegalArgumentException- if fromIndex > toIndexjava.lang.ArrayIndexOutOfBoundsException- if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
-
toString
public static <T> java.lang.String toString(T[] a, ToStringConverter<T> format)
-
toString
public static java.lang.String toString(int[] a, ToStringConverter<java.lang.Integer> format)
-
-
DataMelt 3.0 © DataMelt by jWork.ORG