Java Array Sort sortindices(double[] x)

Here you can find the source of sortindices(double[] x)

Description

Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.

License

Open Source License

Parameter

Parameter Description
a the array to sort by
b the array to re-arrange based on the sort order of the first array.

Declaration

public static final int[] sortindices(double[] x) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  ww  w .j a va 2  s  .c  o m
     * Arrays with lengths beneath this value will be insertion sorted.
     */
    protected static final int SORT_THRESHOLD = 30;

    /**
      * Sort two arrays simultaneously, using the sort order of the values
      * in the first array to determine the sort order for both arrays.
      * @param a the array to sort by
      * @param b the array to re-arrange based on the sort order of the
      * first array.
      */
    public static final int[] sortindices(double[] x) {
        double a[] = new double[x.length];
        int b[] = new int[a.length];
        for (int i = 0; i < a.length; ++i) {
            a[i] = x[i];
            b[i] = i;
        }
        mergesort(a, b, 0, a.length - 1);
        return b;
    }

    /**
      * Sort two arrays simultaneously, using the sort order of the values
      * in the first array to determine the sort order for both arrays.
      * @param a the array to sort by
      * @param b the array to re-arrange based on the sort order of the
      * first array.
      */
    public static final int[] sortindices(long[] x) {
        long a[] = new long[x.length];
        int b[] = new int[a.length];
        for (int i = 0; i < a.length; ++i) {
            a[i] = x[i];
            b[i] = i;
        }
        mergesort(a, b, 0, a.length - 1);
        return b;
    }

    /**
      * Sort two arrays simultaneously, using the sort order of the values
      * in the first array to determine the sort order for both arrays.
      * @param a the array to sort by
      * @param b the array to re-arrange based on the sort order of the
      * first array.
      */
    public static final int[] sortindices(int[] x) {
        int a[] = new int[x.length];
        int b[] = new int[a.length];
        for (int i = 0; i < a.length; ++i) {
            a[i] = x[i];
            b[i] = i;
        }
        mergesort(a, b, 0, a.length - 1);
        return b;
    }

    /**
     * Arrays with lengths beneath this value will be insertion sorted.
     */
    protected static final void mergesort(double[] a, int[] b, int p, int r) {
        if (p >= r) {
            return;
        }
        if (r - p + 1 < SORT_THRESHOLD) {
            insertionsort(a, b, p, r);
        } else {
            int q = (p + r) / 2;
            mergesort(a, b, p, q);
            mergesort(a, b, q + 1, r);
            merge(a, b, p, q, r);
        }
    }

    /**
     * Arrays with lengths beneath this value will be insertion sorted.
     */
    protected static final void mergesort(long[] a, int[] b, int p, int r) {
        if (p >= r) {
            return;
        }
        if (r - p + 1 < SORT_THRESHOLD) {
            insertionsort(a, b, p, r);
        } else {
            int q = (p + r) / 2;
            mergesort(a, b, p, q);
            mergesort(a, b, q + 1, r);
            merge(a, b, p, q, r);
        }
    }

    protected static final void mergesort(int[] a, int[] b, int p, int r) {
        if (p >= r) {
            return;
        }
        if (r - p + 1 < SORT_THRESHOLD) {
            insertionsort(a, b, p, r);
        } else {
            int q = (p + r) / 2;
            mergesort(a, b, p, q);
            mergesort(a, b, q + 1, r);
            merge(a, b, p, q, r);
        }
    }

    protected static final void insertionsort(double[] a, int[] b, int p, int r) {
        for (int j = p + 1; j <= r; ++j) {
            double key = a[j];
            int val = b[j];
            int i = j - 1;
            while (i >= p && a[i] > key) {
                a[i + 1] = a[i];
                b[i + 1] = b[i];
                i--;
            }
            a[i + 1] = key;
            b[i + 1] = val;
        }
    }

    protected static final void insertionsort(long[] a, int[] b, int p, int r) {
        for (int j = p + 1; j <= r; ++j) {
            long key = a[j];
            int val = b[j];
            int i = j - 1;
            while (i >= p && a[i] > key) {
                a[i + 1] = a[i];
                b[i + 1] = b[i];
                i--;
            }
            a[i + 1] = key;
            b[i + 1] = val;
        }
    }

    protected static final void insertionsort(int[] a, int[] b, int p, int r) {
        for (int j = p + 1; j <= r; ++j) {
            int key = a[j];
            int val = b[j];
            int i = j - 1;
            while (i >= p && a[i] > key) {
                a[i + 1] = a[i];
                b[i + 1] = b[i];
                i--;
            }
            a[i + 1] = key;
            b[i + 1] = val;
        }
    }

    protected static final void merge(double[] a, int[] b, int p, int q, int r) {
        double[] t = new double[r - p + 1];
        int[] v = new int[r - p + 1];
        int i, p1 = p, p2 = q + 1;
        for (i = 0; p1 <= q && p2 <= r; ++i) {
            if (a[p1] < a[p2]) {
                v[i] = b[p1];
                t[i] = a[p1++];
            } else {
                v[i] = b[p2];
                t[i] = a[p2++];
            }
        }
        for (; p1 <= q; ++p1, ++i) {
            v[i] = b[p1];
            t[i] = a[p1];
        }
        for (; p2 <= r; ++p2, ++i) {
            v[i] = b[p2];
            t[i] = a[p2];
        }
        for (i = 0, p1 = p; i < t.length; ++i, ++p1) {
            b[p1] = v[i];
            a[p1] = t[i];
        }
    }

    protected static final void merge(long[] a, int[] b, int p, int q, int r) {
        long[] t = new long[r - p + 1];
        int[] v = new int[r - p + 1];
        int i, p1 = p, p2 = q + 1;
        for (i = 0; p1 <= q && p2 <= r; ++i) {
            if (a[p1] < a[p2]) {
                v[i] = b[p1];
                t[i] = a[p1++];
            } else {
                v[i] = b[p2];
                t[i] = a[p2++];
            }
        }
        for (; p1 <= q; ++p1, ++i) {
            v[i] = b[p1];
            t[i] = a[p1];
        }
        for (; p2 <= r; ++p2, ++i) {
            v[i] = b[p2];
            t[i] = a[p2];
        }
        for (i = 0, p1 = p; i < t.length; ++i, ++p1) {
            b[p1] = v[i];
            a[p1] = t[i];
        }
    }

    protected static final void merge(int[] a, int[] b, int p, int q, int r) {
        int[] t = new int[r - p + 1];
        int[] v = new int[r - p + 1];
        int i, p1 = p, p2 = q + 1;
        for (i = 0; p1 <= q && p2 <= r; ++i) {
            if (a[p1] < a[p2]) {
                v[i] = b[p1];
                t[i] = a[p1++];
            } else {
                v[i] = b[p2];
                t[i] = a[p2++];
            }
        }
        for (; p1 <= q; ++p1, ++i) {
            v[i] = b[p1];
            t[i] = a[p1];
        }
        for (; p2 <= r; ++p2, ++i) {
            v[i] = b[p2];
            t[i] = a[p2];
        }
        for (i = 0, p1 = p; i < t.length; ++i, ++p1) {
            b[p1] = v[i];
            a[p1] = t[i];
        }
    }
}

Related

  1. sortEigenValues(int n, double[] d, double[][] v)
  2. sortIndex(double[] A)
  3. sortIndex(double[] doubleArray)
  4. sortIndexes(final T[] array)
  5. sortIndexesDescending(final double[] in)
  6. sortIndices(float[] main)
  7. sortInPlace(final double[] v, final int i, final int j)
  8. sortInPlace(int[] array)
  9. sortInterval(byte[] x, int start, int end)