Java Utililty Methods Array Sort

List of utility methods to do Array Sort

Description

The list of methods to do Array Sort are organized into topic(s).

Method

int[]addSorted(int[] array, int[] newValues)
add Sorted
if (newValues.length == 0) {
    return array;
int[] interim = new int[array.length + newValues.length];
System.arraycopy(array, 0, interim, 0, array.length);
System.arraycopy(newValues, 0, interim, array.length, newValues.length);
Arrays.sort(interim);
int count = 1;
...
intaddSortedUnique(T[] array, T object, int start)
Adds an object to an array that presumably contains sorted elements, but only if it isn't found via binary search.
if (Arrays.binarySearch(array, 0, start, object) < 0)
    return addSorted(array, object, start);
else
    return -1;
int[]addToSortedIntArray(int[] a, int value)
insert value into the sorted array a, at the index returned by java.util.Arrays.binarySearch()
if (a == null || a.length == 0) {
    return new int[] { value };
int insertionPoint = -java.util.Arrays.binarySearch(a, value) - 1;
if (insertionPoint < 0) {
    throw new IllegalArgumentException(String.format("Element %d already exists in array", value));
int[] array = new int[a.length + 1];
...
voidbitReversalSort(final short[] real)
bit Reversal Sort
final int mapping[][] = new int[real.length][2];
for (int i = 0; i < mapping.length; ++i) {
    mapping[i][0] = i;
    mapping[i][1] = bitReverse31(i);
Arrays.sort(mapping, intPairComparator);
for (int i = 0; i < real.length; ++i) {
    final int j = mapping[i][0];
...
doublecalculatePValueForDataPoint(double dataPoint, double[] sortedNullHypothesisSample)
Calculate a p-value for the data point, given the sorted null distribution sample.
if (sortedNullHypothesisSample.length == 0) {
    throw new IllegalArgumentException("can't calculate a p-value with zero permutations");
} else {
    int searchResult = Arrays.binarySearch(sortedNullHypothesisSample, dataPoint);
    int permutationIndexMarker;
    if (searchResult < 1) {
        permutationIndexMarker = (-searchResult) - 1;
    } else {
...
Object[]collectionToSortedArray(Collection objects)
collection To Sorted Array
Object[] sortedArray = objects.toArray(new Object[] {});
Arrays.sort(sortedArray, new Comparator<Object>() {
    @Override
    public int compare(Object o1, Object o2) {
        return o1.toString().compareTo(o2.toString());
});
return sortedArray;
...
String[]copyAndSort(String[] input)
Copy an sort the input array.
String[] result = new String[input.length];
System.arraycopy(input, 0, result, 0, input.length);
Arrays.sort(result);
return result;
T[]copyAndSort(T[] builtinFunctions)
copy And Sort
T[] result = builtinFunctions.clone();
Arrays.sort(result);
return result;
String[]copySortArray(String[] values)
copy Sort Array
if (values == null) {
    return null;
String[] copy = new String[values.length];
System.arraycopy(values, 0, copy, 0, values.length);
Arrays.sort(copy);
return copy;
voidcountingSort(int[] a, int low, int high)
Counting sort that sorts the integer array in O(n+k) where n is the number of elements and k is the length of the integer intervals given (high - low).
final int[] counts = new int[high - low + 1];
for (int x : a) {
    counts[x - low]++;
int current = 0;
for (int i = 0; i < counts.length; i++) {
    Arrays.fill(a, current, current + counts[i], i + low);
    current += counts[i];
...