Class SortUtils
- Object
-
- 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.order array 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 indexm=(i+j)/2
the median of the values at indicesi,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 ...
-
-
-
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 itemsi
- the first index to be exchangedj
- 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 compareb
- 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 indexm=(i+j)/2
the median of the values at indicesi,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 3i
- the fist index of the range to considerj
- 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 celli
- 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 copyn
- 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 stringn
- 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 returnorder
- the order in which to arrange the numbers- Returns:
- an array of n numbers
-
-