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

T[]sort(T[] array)
sort
T[] output = Arrays.copyOf(array, array.length);
Arrays.sort(output);
return output;
T[]sort(T[] array)
Sort the array of elements according to the elements natural ordering.
Arrays.sort(array);
return array;
T[]sort(T[] array, boolean nullsFirst)
Sorts an array with null elements according to the natural order of the elements.
if (array == null)
    return null;
int nullPos;
if (nullsFirst) {
    nullPos = 0;
    for (int i = 0; i < array.length; i++) {
        if (array[i] == null) {
            if (i != nullPos) {
...
voidsort(T[] src)
sort
mergeSort(src, src, 0, src.length, 0);
voidsort1(int x[], int off, int len, int y[])
sort
if (len < 7) {
    for (int i = off; i < len + off; i++)
        for (int j = i; j > off && x[j - 1] > x[j]; j--) {
            swap(x, j, j - 1);
            swap(y, j, j - 1);
    return;
int m = off + (len >> 1); 
if (len > 7) {
    int l = off;
    int n = off + len - 1;
    if (len > 40) { 
        int s = len / 8;
        l = med3(x, l, l + s, l + 2 * s);
        m = med3(x, m - s, m, m + s);
        n = med3(x, n - 2 * s, n - s, n);
    m = med3(x, l, m, n); 
int v = x[m];
int a = off, b = a, c = off + len - 1, d = c;
while (true) {
    while (b <= c && x[b] <= v) {
        if (x[b] == v) {
            swap(x, a, b);
            swap(y, a++, b);
        b++;
    while (c >= b && x[c] >= v) {
        if (x[c] == v) {
            swap(x, c, d);
            swap(y, c, d--);
        c--;
    if (b > c)
        break;
    swap(x, b, c);
    swap(y, b++, c--);
int s, n = off + len;
s = Math.min(a - off, b - a);
vecswap(x, off, b - s, s, y);
s = Math.min(d - c, n - d - 1);
vecswap(x, b, n - s, s, y);
if ((s = b - a) > 1)
    sort1(x, off, s, y);
if ((s = d - c) > 1)
    sort1(x, n - s, s, y);
voidsort1(long x[], int off, int len)
Sorts the specified sub-array of longs into ascending order.
if (len < 7) {
    for (int i = off; i < len + off; i++)
        for (int j = i; j > off && x[j - 1] > x[j]; j--)
            swap(x, j, j - 1);
    return;
int m = off + (len >> 1); 
if (len > 7) {
...
voidsort2(double v[], int v2[], int left, int right)
sort
int k, last;
if (left >= right)
    return;
swap2(v, v2, left, (left + right) / 2);
last = left;
for (k = left + 1; k <= right; k++)
    if (v[k] < v[left])
        swap2(v, v2, ++last, k);
...
voidsort2(double[] arr, double[] brr)
Sorts an array arr[1..n] into ascending order using Quicksort, while making the corresponding rearrangement of the array brr[1..n].
int n = arr.length;
int i, ir = n - 1, j, k, l = 0;
int[] istack;
int jstack = 0;
double a, b, temp;
double dummy;
istack = new int[50];
for (;;) {
...
voidsort3(String[] a, int x, int y, int z)
Auxiliary method for sorting lexicographically the strings at the positions x, y and z.
if (a[x].compareTo(a[y]) > 0) {
    if (a[x].compareTo(a[z]) > 0) {
        if (a[y].compareTo(a[z]) > 0) {
            swap(a, x, z);
        } else {
            swap3(a, x, y, z);
    } else {
...
voidsort3(String[] arr, float[] brr)
Sorts an array arr[1..n] into ascending order using Quicksort, while making the corresponding rearrangement of the array brr[1..n].
int n = arr.length;
int i, ir = n - 1, j, k, l = 0;
int[] istack;
int jstack = 0;
float b, dummyFloat;
String a, dummyString;
istack = new int[50];
for (;;) {
...