Class SortUtils


  • public class SortUtils
    extends Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <E> E[] arrayAs​(E[] items, int n)
      Creates an array with n cells of the same type as the given array filled with nulls.
      static <E> int compare​(E a, E b)
      Compares the given items.
      static <E> E[] copyCell​(E[] items, int i)
      Returns as 1D array a copy of the i-th cell from the given array.
      static Double[] generateArray​(int n, String order)
      Returns Double primitive array of n elements as listed below.

      Based on this sort comparison by Howard Hinnant.

      orderarray elements
      "sorted"1,2,...,n
      "reverse"n,n-1,n-2,...,1
      "random"random shuffle of 1,2,...,n
      "ms-worst"the worst case for Merge Sort
      "qs-worst-balanced"the worst case for Balanced Quick Sort
      "all-equal"all are same value
      "even-odd"1,2,...,n/2,1,2,...n/2
      "reverse-even-odd"n/2,...,2,1,n/2,...,2,1
      "pipe-organ"1,2,...,n/2,n/2,...,2,1
      "push-front"2,...,n,1
      "push-middle"1,2,...,n,n/2
      static <E> void median3​(E[] items, int i, int j)
      Places at index m=(i+j)/2 the median of the values at indices i,m,j.
      static <E> void swap​(E[] items, int i, int j)
      Exchanges the values of the items at the given indices.
      static <E> String toString​(E[] items)
      Returns a String representation of the given primitive array in this format: [item0 item1 ...
      static <E> String toString​(E[] items, int n)
      Returns a String representation of the first n items in the given primitive array in this format: [item0 item1 ...
      • Methods inherited from class Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • swap

        public static <E> void swap​(E[] items,
                                    int i,
                                    int j)
        Exchanges the values of the items at the given indices.
        Parameters:
        items - the array in which to exchange items
        i - the first index to be exchanged
        j - the second index to be exchanged
      • compare

        public static <E> int compare​(E a,
                                      E b)
        Compares the given items.
        Parameters:
        a - the first item to compare
        b - the second item to compare
        Returns:
        -1, 0, +1 respectively when a <, ==, > b
      • median3

        public static <E> void median3​(E[] items,
                                       int i,
                                       int j)
        Places at index m=(i+j)/2 the median of the values at indices i,m,j. In addition sorts the three values, so that [i]≤[m]≤[j]. Assumes the range is not empty.
        Parameters:
        items - the array for which to compute the median of 3
        i - the fist index of the range to consider
        j - the last index (inclusive) of the range to consider
      • copyCell

        public static <E> E[] copyCell​(E[] items,
                                       int i)
        Returns as 1D array a copy of the i-th cell from the given array.
        Parameters:
        items - the array from which to copy a cell
        i - the index of the cell to copy
        Returns:
        a one-cell array containing the i-th cell of the given array
      • arrayAs

        public static <E> E[] arrayAs​(E[] items,
                                      int n)
        Creates an array with n cells of the same type as the given array filled with nulls. The requested number of cells can be larger or smaller than the size of the array.
        Parameters:
        items - the array to copy
        n - number of cells in the result (no restrictions)
        Returns:
        an array with n cells of the same type as the given array filled with nulls
      • toString

        public static <E> String toString​(E[] items,
                                          int n)
        Returns a String representation of the first n items in the given primitive array in this format: [item0 item1 ... item(n-1)].
        Parameters:
        items - the array of items that is to be convert to a string
        n - the number of items to convert starting from the beginning
        Returns:
        a String representation of the first n items in the given primitive array
      • toString

        public static <E> String toString​(E[] items)
        Returns a String representation of the given primitive array in this format: [item0 item1 ... item(len-1)].
        Parameters:
        items - the array of items that is to be convert to a string
        Returns:
        a String representation of the given primitive array
      • generateArray

        public static Double[] generateArray​(int n,
                                             String order)
        Returns Double primitive array of n elements (see above).
        Parameters:
        n - the number of items to return
        order - the order in which to arrange the numbers
        Returns:
        an array of n numbers