Class ArraysUtil


  • public final class ArraysUtil
    extends Object
    This class contains additional methods for manipulating arrays (such as sorting and searching). For all quick sort methods the base code was taken from jdk6 Arrays class.
    Author:
    Dmitry Bolotin, Stanislav Poslavsky
    See Also:
    Arrays
    • Method Summary

      Modifier and Type Method Description
      static int[] addAll​(int[]... arrays)  
      static int[] addAll​(int[] array1, int... array2)  
      static long[] addAll​(long[] array1, long... array2)  
      static <T> T[] addAll​(T[] array1, T... array2)
      This code is taken from Apache Commons Lang ArrayUtils.
      static char[] arrayOf​(char val, int len)  
      static int[] arrayOf​(int val, int len)  
      static long[] arrayOf​(long val, int len)  
      static <T> T[] arrayOf​(T val, int len)  
      static <T extends 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, Comparator<? super T> comparator)
      This method is similar to bijection(Comparable[], Comparable[]) }, but uses specified comparator.
      static int binarySearch1​(int[] a, int key)
      This is the same method to Arrays.binarySearch(int[], int).
      static int binarySearch1​(int[] a, int fromIndex, int toIndex, int key)
      This is the same method to Arrays.binarySearch(int[], int, int, int).
      static int[] byte2int​(byte[] a)  
      static short[] byte2short​(byte[] a)  
      static int commutativeHashCode​(int[] data)
      Returns commutative hash code of the data
      static int commutativeHashCode​(int[] data, int from, int to)
      Returns commutative hash code of the data
      static <T> int commutativeHashCode​(T[] data)
      Returns commutative hash code of the data
      static <T> int commutativeHashCode​(T[] data, int from, int to)
      Returns commutative hash code of the data
      static int[][] deepClone​(int[][] input)  
      static Object[][] deepClone​(Object[][] input)  
      static int firstIndexOf​(int element, int[] array)  
      static int firstIndexOf​(Object element, Object[] array)  
      static int[] flatten​(int[][] array)  
      static int[] getSortedDistinct​(int[] values)
      Sort array & return array with removed repetitive values.
      static long[] getSortedDistinct​(long[] values)
      Sort array & return array with removed repetitive values.
      static BigInteger[] getSortedDistinct​(BigInteger[] values)
      Sort array & return array with removed repetitive values.
      static int indexOfMax​(int[] array)  
      static int[] insert​(int[] array, int position, int value)  
      static int[] insert​(int[] array, int position, int value, int length)  
      static long[] insert​(long[] array, int position, long value)  
      static <T> T[] insert​(T[] array, int position, T value)  
      static void insertionSort​(int[] target, int[] coSort)
      Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes the coSort ints array in the same way as the target array.
      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 the coSort ints array in the same way as the target array.
      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 the coSort ints array in the same way as the target array.
      static void insertionSort​(int[] target, long[] coSort)
      Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes the coSort longs array in the same way as the specified target array.
      static <T extends Comparable<T>>
      void
      insertionSort​(T[] target, int[] 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 the coSort objects array in the same way then specified target array.
      static <T extends Comparable<T>>
      void
      insertionSort​(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 using insertion sort algorithm and simultaneously permutes the coSort objects array in the same way then specified target array.
      static <T extends Comparable<T>>
      void
      insertionSort​(T[] target, int fromIndex, int toIndex, 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 the coSort objects array in the same way then specified target array.
      static <T extends Comparable<T>>
      void
      insertionSort​(T[] target, 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 the coSort objects array in the same way then specified target array.
      static byte[] int2byte​(int[] a)  
      static short[] int2short​(int[] a)  
      static int[] intSetDifference​(int[] main, int[] delete)
      Return the set difference B - A for int sets A and B.
      Sets A and B must be represented as two sorted int arrays.
      Repetitive values in A or B not allowed.
      static int[] intSetUnion​(int[] a, int[] b)
      Return the union B + A for integer sets A and B.
      Sets A and B must be represented as two sorted integer arrays.
      Repetitive values in A or B not allowed.
      static int max​(int[] array)  
      static int[] max​(int[] a, int[] b)  
      static int max​(int[] array, int from, int to)  
      static long max​(long[] array)  
      static int min​(int[] array)  
      static int[] min​(int[] a, int[] b)  
      static int min​(int[] array, int from, int to)  
      static long min​(long[] array)  
      static int[] multiply​(int[] a, int[] b)  
      static int multiply​(int[] array, int from, int to)  
      static double multiplyToDouble​(int[] array)  
      static double multiplyToDouble​(int[] array, int from, int to)  
      static int[] negate​(int[] arr)  
      static long[] negate​(long[] arr)  
      static BigInteger[] negate​(BigInteger[] arr)  
      static int or​(long[] array)  
      static int or​(long[] array, int from)  
      static int or​(long[] array, int from, int to)  
      static void quickSort​(int[] target, int[] coSort)
      Sorts the specified target array of ints into ascending numerical order and simultaneously permutes the coSort ints array in the same way as the target array.
      static void quickSort​(int[] target, int[] cosort, IntComparator comparator)
      Sorts the specified target array of ints according to IntComparator and simultaneously permutes the coSort Objects array in the same way as the target array.
      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 the coSort ints array in the same way as the target array.
      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 to IntComparator and simultaneously permutes the coSort Objects array in the same way as the target array.
      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 the coSort longs array in the same way as the target array.
      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 by IntComparator using quicksort.
      static void quickSort​(int[] target, int fromIndex, int toIndex, Object[] coSort)
      Sorts the specified range of the specified target array of ints into ascending numerical order and simultaneously permutes the coSort Objects array in the same way as the target array.
      static void quickSort​(int[] target, long[] coSort)
      Sorts the specified target array of ints into ascending numerical order and simultaneously permutes the coSort longs array in the same way as the target array.
      static void quickSort​(int[] target, IntComparator comparator)
      Sorts the specified range of the specified target array of ints into order specified by IntComparator using quicksort.
      static void quickSort​(int[] target, Object[] coSort)
      Sorts the specified target array of ints into ascending numerical order and simultaneously permutes the coSort Objects array in the same way as the target array.
      static void quickSort​(long[] 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 the coSort longs array in the same way as the target array.
      static void quickSort​(long[] target, long[] coSort)
      Sorts the specified target array of ints into ascending numerical order and simultaneously permutes the coSort longs array in the same way as the target array.
      static void quickSort​(short[] target, int[] coSort)
      Sorts the specified target array of shorts into ascending numerical order and simultaneously permutes the coSort ints array in the same way as the target array.
      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 the coSort ints array in the same way as the target array.
      static <T extends 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 the coSort objects array in the same way then specified target array.
      static <T extends 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 the coSort objects array in the same way then specified target array.
      static <T extends Comparable<T>>
      void
      quickSort​(T[] target, int fromIndex, int toIndex, Object[] coSort)
      Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements and simultaneously permutes the coSort objects array in the same way then specified target array.
      static <T extends Comparable<T>>
      void
      quickSort​(T[] target, Object[] coSort)
      Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements and simultaneously permutes the coSort objects array in the same way then specified target array.
      static void quickSort1​(int[] target, int fromIndex, int length, int[] coSort)
      This method is the same as quickSort(int[], int, int, int[]), but without range checking and toIndex -> length (see params).
      static void quickSort1​(int[] target, int fromIndex, int length, long[] coSort)
      This method is the same as ) , but without range checking.
      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 by IntComparator using quicksort.
      static void quickSort1​(int[] target, int fromIndex, int length, Object[] coSort)
      This method is the same as ) , but without range checking.
      static void quickSort1​(long[] target, int fromIndex, int length, long[] coSort)
      This method is the same as ) , but without range checking.
      static void quickSort1​(short[] target, int fromIndex, int length, int[] coSort)
      This method is the same as quickSort(int[], int, int, int[]), but without range checking and toIndex -> length (see params).
      static <T extends Comparable<T>>
      void
      quickSort1​(T[] target, int fromIndex, int length, int[] coSort)
      This method is the same as quickSort(Comparable[], int, int, Object[]), but without range checking.
      static <T extends Comparable<T>>
      void
      quickSort1​(T[] target, int fromIndex, int length, Object[] coSort)
      This method is the same as quickSort(Comparable[], int, int, Object[]), but without range checking.
      static int[] quickSortP​(int[] target)
      Sorts the specified array and returns the resulting permutation
      static int[] quickSortP​(short[] target)  
      static int[] remove​(int[] array, int i)  
      static int[] remove​(int[] array, int[] positions)
      Removes elements at specified positions in specified array.
      static long[] remove​(long[] array, int i)  
      static long[] remove​(long[] array, int[] positions)
      Removes elements at specified positions in specified array.
      static <T> T[] remove​(T[] array, int i)  
      static <T> T[] remove​(T[] array, int[] positions)
      Removes elements at specified positions in specified array.
      static void reverse​(int[] array)  
      static void reverse​(int[] array, int from, int to)  
      static void reverse​(long[] array)  
      static void reverse​(long[] array, int from, int to)  
      static <T> void reverse​(T[] array)  
      static <T> void reverse​(T[] array, int from, int to)  
      static int[] select​(int[] array, int[] positions)
      Selects elements from specified array at specified positions.
      static <T> T[] select​(T[] array, int[] positions)
      Selects elements from specified array at specified positions.
      static int[] sequence​(int size)  
      static int[] sequence​(int from, int to)  
      static int[] short2int​(short[] a)  
      static void shuffle​(int[] array, org.apache.commons.math3.random.RandomGenerator rnd)  
      static void stableSort​(int[] target, int[] cosort)
      Sorts the specified array of ints into ascending order using stable sort algorithm and simultaneously permutes the coSort ints array in the same way as the target array.
      static int[] subtract​(int[] a, int[] b)  
      static int sum​(int[] array)  
      static int sum​(int[] array, int from)  
      static int[] sum​(int[] a, int[] b)  
      static int sum​(int[] array, int from, int to)  
      static double sumToDouble​(int[] array)  
      static double sumToDouble​(int[] array, int from, int to)  
      static void swap​(int[] x, int a, int b)
      Swaps x[a] with x[b].
      static void swap​(long[] x, int a, int b)
      Swaps x[a] with x[b].
      static void swap​(Object[] x, int a, int b)
      Swaps x[a] with x[b].
      static void timSort​(int[] target, int[] coSort)
      Sorts the specified array of ints into ascending order using TimSort algorithm and simultaneously permutes the coSort ints array in the same way as the target array.
      static int[] toArray​(Set<Integer> set)
      Converts Set<Integer> to int[]
      static String toString​(long[] a, int from, int to)  
      static <T> String toString​(T[] a, int from, int to)  
      static <T> String toString​(T[] a, int from, int to, Function<T,​String> stringer)  
    • Field Detail

      • COMPARATOR

        public static final Comparator<int[]> COMPARATOR
        Lexicographic order
      • COMPARATOR_LONG

        public static final Comparator<long[]> COMPARATOR_LONG
        Lexicographic order
      • COMPARATOR_GENERIC

        public static final Comparator<Comparable[]> COMPARATOR_GENERIC
        Lexicographic order
    • Method Detail

      • flatten

        public static int[] flatten​(int[][] array)
      • arrayOf

        public static int[] arrayOf​(int val,
                                    int len)
      • arrayOf

        public static long[] arrayOf​(long val,
                                     int len)
      • arrayOf

        public static char[] arrayOf​(char val,
                                     int len)
      • arrayOf

        public static <T> T[] arrayOf​(T val,
                                      int len)
      • negate

        public static int[] negate​(int[] arr)
      • negate

        public static long[] negate​(long[] arr)
      • toString

        public static String toString​(long[] a,
                                      int from,
                                      int to)
      • toString

        public static <T> String toString​(T[] a,
                                          int from,
                                          int to)
      • toString

        public static <T> String toString​(T[] a,
                                          int from,
                                          int to,
                                          Function<T,​String> stringer)
      • shuffle

        public static void shuffle​(int[] array,
                                   org.apache.commons.math3.random.RandomGenerator rnd)
      • getSortedDistinct

        public static int[] getSortedDistinct​(int[] values)
        Sort array & return array with removed repetitive values.
        Parameters:
        values - input array (this method will quickSort this array)
        Returns:
        sorted array of distinct values
      • getSortedDistinct

        public static BigInteger[] getSortedDistinct​(BigInteger[] values)
        Sort array & return array with removed repetitive values.
        Parameters:
        values - input array (this method will quickSort this array)
        Returns:
        sorted array of distinct values
      • getSortedDistinct

        public static long[] getSortedDistinct​(long[] values)
        Sort array & return array with removed repetitive values.
        Parameters:
        values - input array (this method will quickSort this array)
        Returns:
        sorted array of distinct values
      • intSetDifference

        public static int[] intSetDifference​(int[] main,
                                             int[] delete)
        Return the set difference B - A for int sets A and B.
        Sets A and B must be represented as two sorted int arrays.
        Repetitive values in A or B not allowed.
        Parameters:
        main - sorted array of distinct values (set B)
        delete - sorted array of distinct values (set A)
        Returns:
        the set of elements in B but not in A
      • intSetUnion

        public static int[] intSetUnion​(int[] a,
                                        int[] b)
        Return the union B + A for integer sets A and B.
        Sets A and B must be represented as two sorted integer arrays.
        Repetitive values in A or B not allowed.
        Parameters:
        a - sorted array of distinct values. (set A)
        b - sorted array of distinct values. (set B)
        Returns:
        the set of elements from B and from A
      • insert

        public static int[] insert​(int[] array,
                                   int position,
                                   int value)
      • insert

        public static int[] insert​(int[] array,
                                   int position,
                                   int value,
                                   int length)
      • insert

        public static long[] insert​(long[] array,
                                    int position,
                                    long value)
      • insert

        public static <T> T[] insert​(T[] array,
                                     int position,
                                     T value)
      • reverse

        public static void reverse​(int[] array,
                                   int from,
                                   int to)
      • reverse

        public static void reverse​(long[] array,
                                   int from,
                                   int to)
      • reverse

        public static <T> void reverse​(T[] array,
                                       int from,
                                       int to)
      • reverse

        public static <T> void reverse​(T[] array)
      • reverse

        public static void reverse​(int[] array)
      • reverse

        public static void reverse​(long[] array)
      • 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)
      • max

        public static long max​(long[] array)
      • max

        public static int max​(int[] array,
                              int from,
                              int to)
      • max

        public static int[] max​(int[] a,
                                int[] b)
      • min

        public static int min​(int[] array)
      • min

        public static long min​(long[] array)
      • min

        public static int min​(int[] array,
                              int from,
                              int to)
      • min

        public static int[] min​(int[] a,
                                int[] b)
      • firstIndexOf

        public static int firstIndexOf​(int element,
                                       int[] array)
      • firstIndexOf

        public static int firstIndexOf​(Object element,
                                       Object[] array)
      • indexOfMax

        public static int indexOfMax​(int[] array)
      • sequence

        public static int[] sequence​(int size)
      • sequence

        public static int[] sequence​(int from,
                                     int to)
      • deepClone

        public static int[][] deepClone​(int[][] input)
      • deepClone

        public static Object[][] deepClone​(Object[][] input)
      • sum

        public static int sum​(int[] array)
      • sum

        public static int[] sum​(int[] a,
                                int[] b)
      • multiply

        public static int[] multiply​(int[] a,
                                     int[] b)
      • subtract

        public static int[] subtract​(int[] a,
                                     int[] b)
      • sum

        public static int sum​(int[] array,
                              int from)
      • sum

        public static int sum​(int[] array,
                              int from,
                              int to)
      • multiply

        public static int multiply​(int[] array,
                                   int from,
                                   int to)
      • multiplyToDouble

        public static double multiplyToDouble​(int[] array,
                                              int from,
                                              int to)
      • multiplyToDouble

        public static double multiplyToDouble​(int[] array)
      • sumToDouble

        public static double sumToDouble​(int[] array,
                                         int from,
                                         int to)
      • sumToDouble

        public static double sumToDouble​(int[] array)
      • or

        public static int or​(long[] array)
      • or

        public static int or​(long[] array,
                             int from)
      • or

        public static int or​(long[] array,
                             int from,
                             int to)
      • bijection

        public static <T> int[] bijection​(T[] from,
                                          T[] to,
                                          Comparator<? super T> comparator)
        This method is similar to bijection(Comparable[], Comparable[]) }, but uses specified comparator.
        Parameters:
        from - from array
        to - to array
        comparator - comparator
        Returns:
        a bijective mapping from from-array to to-array and null if no mapping exist
      • bijection

        public static <T extends 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 array
        to - to array
        Returns:
        a bijective mapping from from-array to to-array and null if no mapping exist
      • addAll

        public static int[] addAll​(int[] array1,
                                   int... array2)
      • addAll

        public static long[] addAll​(long[] array1,
                                    long... array2)
      • addAll

        public static int[] addAll​(int[]... arrays)
      • remove

        public static int[] remove​(int[] array,
                                   int i)
      • remove

        public static long[] remove​(long[] array,
                                    int i)
      • 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 array1 followed by all of the elements array2. 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 be null
        array2 - the second array whose elements are added to the new array, may be null
        Returns:
        The new array, null if both arrays are null. 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:
        IllegalArgumentException - if the array types are incompatible
        Since:
        2.1
      • remove

        public static <T> T[] remove​(T[] array,
                                     int[] positions)
        Removes elements at specified positions in specified array. This method preserve the relative order of elements in specified array.
        Type Parameters:
        T - generic type
        Parameters:
        array - array of elements
        positions - positions of elements that should be removed
        Returns:
        new array with removed elements at specified positions
        Throws:
        ArrayIndexOutOfBoundsException - if some position larger then array length
      • remove

        public static int[] remove​(int[] array,
                                   int[] positions)
        Removes elements at specified positions in specified array. This method preserve the relative order of elements in specified array.
        Parameters:
        array - array of elements
        positions - positions of elements that should be removed
        Returns:
        new array with removed elements at specified positions
        Throws:
        ArrayIndexOutOfBoundsException - if some position larger then array length
      • remove

        public static long[] remove​(long[] array,
                                    int[] positions)
        Removes elements at specified positions in specified array. This method preserve the relative order of elements in specified array.
        Parameters:
        array - array of elements
        positions - positions of elements that should be removed
        Returns:
        new array with removed elements at specified positions
        Throws:
        ArrayIndexOutOfBoundsException - if some position larger then array length
      • select

        public static <T> T[] select​(T[] array,
                                     int[] positions)
        Selects elements from specified array at specified positions. The resulting array preserves the relative order of elements in specified array.
        Type Parameters:
        T - generic type
        Parameters:
        array - array of elements
        positions - of elements that should be picked out
        Returns:
        the array of elements that picked out from specified positions in specified array
      • select

        public static int[] select​(int[] array,
                                   int[] positions)
        Selects elements from specified array at specified positions. The resulting array preserves the relative order of elements in specified array.
        Parameters:
        array - array of elements
        positions - 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​(Set<Integer> set)
        Converts Set<Integer> to int[]
        Parameters:
        set - a Set of Integer
        Returns:
        int[]
      • binarySearch1

        public static int binarySearch1​(int[] a,
                                        int key)
        This is the same method to Arrays.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 returns low).
        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. 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 to Arrays.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 returns low).
        Parameters:
        a - the array to be searched
        key - the value to be searched for
        fromIndex - the index of the first element (inclusive) to be searched
        toIndex - 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 <T> int commutativeHashCode​(T[] data)
        Returns commutative hash code of the data
        Parameters:
        data - array
        Returns:
        commutative hash
      • commutativeHashCode

        public static <T> int commutativeHashCode​(T[] data,
                                                  int from,
                                                  int to)
        Returns commutative hash code of the data
        Parameters:
        data - array
        Returns:
        commutative hash
      • commutativeHashCode

        public static int commutativeHashCode​(int[] data)
        Returns commutative hash code of the data
        Parameters:
        data - array
        Returns:
        commutative hash
      • commutativeHashCode

        public static int commutativeHashCode​(int[] data,
                                              int from,
                                              int to)
        Returns commutative hash code of the data
        Parameters:
        data - 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 the coSort ints 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 sorted
        coSort - the array which will be permuted in the same way as the target array during sorting procedure
        Throws:
        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 the coSort ints 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        coSort - the array which will be permuted in the same way as the target array, during sorting procedure
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        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 the coSort longs 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 sorted
        coSort - the array which will be permuted in the same way as the target array during sorting procedure
        Throws:
        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 the coSort ints 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        coSort - the array which will be permuted in the same way as the specified target array, during sorting procedure
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
      • insertionSort

        public static <T extends Comparable<T>> void insertionSort​(T[] target,
                                                                   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 the coSort objects 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 sorted
        coSort - the array which will be permuted in the same way as the target array during sorting procedure
        Throws:
        IllegalArgumentException - if coSort length less then target length.
      • insertionSort

        public static <T extends Comparable<T>> void insertionSort​(T[] target,
                                                                   int fromIndex,
                                                                   int toIndex,
                                                                   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 the coSort objects 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        coSort - the array which will be permuted in the same way as the target array, during sorting procedure
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
      • insertionSort

        public static <T extends Comparable<T>> void insertionSort​(T[] target,
                                                                   int[] 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 the coSort objects 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 sorted
        coSort - the array which will be permuted in the same way as the target array during sorting procedure
        Throws:
        IllegalArgumentException - if coSort length less then target length.
      • insertionSort

        public static <T extends Comparable<T>> void insertionSort​(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 using insertion sort algorithm and simultaneously permutes the coSort objects 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        coSort - the array which will be permuted in the same way as the target array, during sorting procedure
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        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 the coSort ints 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 sorted
        coSort - the array which will be permuted in the same way as the target array during sorting procedure
        Throws:
        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 the coSort ints 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 sorted
        cosort - the array which will be permuted in the same way as the target array during sorting procedure
        Throws:
        ClassCastException - if the array contains elements that are not mutually comparable (for example, strings and integers)
        IllegalArgumentException - if coSort length less then target length.
        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 the coSort ints 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 coSort array 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 IllegalArgumentException if target == coSort, because in this case no sorting will be perfomed.

        Parameters:
        target - the array to be sorted
        coSort - the array which will be permuted in the same way as the target array during sorting procedure
        Throws:
        IllegalArgumentException - if coSort length less then target length.
        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 the coSort ints 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 coSort array 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 IllegalArgumentException if target == coSort, because in this case no sorting will be perfomed.

        Parameters:
        target - the array to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        coSort - the array which will be permuted in the same way as the target array, during sorting procedure
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
        IllegalArgumentException - if target == coSort (as references).
      • quickSort1

        public static void quickSort1​(int[] target,
                                      int fromIndex,
                                      int length,
                                      int[] coSort)
        This method is the same as quickSort(int[], int, int, int[]), but without range checking and toIndex -> length (see params). Throws IllegalArgumentException if target == coSort, because in this case no sorting will be perfomed .

        NOTE: this is unstable sort algorithm, so additional combinatorics of the coSort array 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        length - 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:
        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​(long[] target,
                                     long[] coSort)
        Sorts the specified target array of ints into ascending numerical order and simultaneously permutes the coSort longs 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 coSort array 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 sorted
        coSort - the array which will be permuted in the same way as the target array during sorting procedure
        Throws:
        IllegalArgumentException - if coSort length less then target length.
      • quickSort

        public static void quickSort​(long[] 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 the coSort longs 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 coSort array 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        coSort - the array which will be permuted in the same way as the target array, during sorting procedure
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
      • quickSort1

        public static void quickSort1​(long[] 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 coSort array 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        length - 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​(int[] target,
                                     long[] coSort)
        Sorts the specified target array of ints into ascending numerical order and simultaneously permutes the coSort longs 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 coSort array 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 sorted
        coSort - the array which will be permuted in the same way as the target array during sorting procedure
        Throws:
        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 the coSort longs 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 coSort array 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        coSort - the array which will be permuted in the same way as the target array, during sorting procedure
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        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 coSort array 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        length - the length of the sorting subarray.
        coSort - the array which will be permuted in the same way as the target array, during sorting procedure
      • swap

        public static void swap​(long[] x,
                                int a,
                                int b)
        Swaps x[a] with x[b].
      • quickSort

        public static <T extends Comparable<T>> void quickSort​(T[] target,
                                                               Object[] coSort)
        Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements and simultaneously permutes the coSort objects 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 coSort array 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 sorted
        coSort - the array which will be permuted in the same way as the target array during sorting procedure
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        IllegalArgumentException - if coSort length less then target length.
        IllegalArgumentException - if target == coSort (as references).
      • quickSort

        public static <T extends Comparable<T>> void quickSort​(T[] target,
                                                               int fromIndex,
                                                               int toIndex,
                                                               Object[] coSort)
        Sorts the specified target array of objects into ascending order, according to the natural ordering of its elements and simultaneously permutes the coSort objects 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 coSort array 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        coSort - the array which will be permuted in the same way as the target array, during sorting procedure
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
        IllegalArgumentException - if target == coSort (as references).
      • quickSort1

        public static <T extends Comparable<T>> void quickSort1​(T[] target,
                                                                int fromIndex,
                                                                int length,
                                                                Object[] coSort)
        This method is the same as quickSort(Comparable[], int, int, Object[]), but without range checking.

        NOTE: this is unstable sort algorithm, so additional combinatorics of the coSort array 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        length - 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:
        IllegalArgumentException - if target == coSort (as references).
      • quickSort

        public static <T extends 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 the coSort objects 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 coSort array 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 sorted
        coSort - the array which will be permuted in the same way as the target array during sorting procedure
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        IllegalArgumentException - if coSort length less then target length.
        IllegalArgumentException - if target == coSort (as references).
      • quickSort

        public static <T extends 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 the coSort objects 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 coSort array 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        coSort - the array which will be permuted in the same way as the target array, during sorting procedure
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
        IllegalArgumentException - if target == coSort (as references).
      • quickSort1

        public static <T extends Comparable<T>> void quickSort1​(T[] target,
                                                                int fromIndex,
                                                                int length,
                                                                int[] coSort)
        This method is the same as quickSort(Comparable[], int, int, Object[]), but without range checking.

        NOTE: this is unstable sort algorithm, so additional combinatorics of the coSort array 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        length - 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:
        IllegalArgumentException - if target == coSort (as references).
      • swap

        public static void swap​(Object[] x,
                                int a,
                                int b)
        Swaps x[a] with x[b].
      • quickSort

        public static void quickSort​(int[] target,
                                     Object[] coSort)
        Sorts the specified target array of ints into ascending numerical order and simultaneously permutes the coSort Objects 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 coSort array 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 sorted
        coSort - the array which will be permuted in the same way as the target array during sorting procedure
        Throws:
        IllegalArgumentException - if coSort length less then target length.
      • quickSort

        public static void quickSort​(int[] target,
                                     int fromIndex,
                                     int toIndex,
                                     Object[] coSort)
        Sorts the specified range of the specified target array of ints into ascending numerical order and simultaneously permutes the coSort Objects 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 coSort array 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        coSort - the array which will be permuted in the same way as the target array, during sorting procedure
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length
      • quickSort1

        public static void quickSort1​(int[] target,
                                      int fromIndex,
                                      int length,
                                      Object[] coSort)
        This method is the same as ) , but without range checking.

        NOTE: this is unstable sort algorithm, so additional combinatorics of the coSort array 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        length - 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 the coSort ints 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 coSort array 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 IllegalArgumentException if target == coSort, because in this case no sorting will be perfomed.

        Parameters:
        target - the array to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        coSort - the array which will be permuted in the same way as the target array, during sorting procedure
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        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 as quickSort(int[], int, int, int[]), but without range checking and toIndex -> length (see params). Throws IllegalArgumentException if target == coSort, because in this case no sorting will be perfomed .

        NOTE: this is unstable sort algorithm, so additional combinatorics of the coSort array 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        length - 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 the coSort ints 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 coSort array 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 IllegalArgumentException if target == coSort, because in this case no sorting will be perfomed.

        Parameters:
        target - the array to be sorted
        coSort - the array which will be permuted in the same way as the target array during sorting procedure
        Throws:
        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 by IntComparator using quicksort.
        Parameters:
        target - the array to be sorted
        comparator - custom comparator
        Throws:
        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 by IntComparator using quicksort.
        Parameters:
        target - the array to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        comparator - comparator
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        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 by IntComparator using quicksort.
        Parameters:
        target - the array to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        length - 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 to IntComparator and simultaneously permutes the coSort Objects 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 coSort array 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 sorted
        comparator - custom comparator
        Throws:
        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 to IntComparator and simultaneously permutes the coSort Objects 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 coSort array 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 sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        comparator - comparator
        Throws:
        IllegalArgumentException - if fromIndex > toIndex
        ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > target.length or toIndex > coSort.length