Java Array Sort sort(double[] a, int[] b)

Here you can find the source of sort(double[] a, int[] b)

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 void sort(double[] a, int[] b) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w  w w  . j a va  2  s  . co 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 void sort(double[] a, int[] b) {
        mergesort(a, b, 0, a.length - 1);
    }

    /**
     * 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.
     * @param length the array range length to sort over
     */
    public static final void sort(double[] a, int[] b, int length) {
        mergesort(a, b, 0, length - 1);
    }

    /**
     * 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.
     * @param begin the start index of the range to sort
     * @param end the end index, exclusive, of the range to sort
     */
    public static final void sort(double[] a, int[] b, int begin, int end) {
        mergesort(a, b, begin, end - 1);
    }

    /**
     * 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 void sort(long[] a, int[] b) {
        mergesort(a, b, 0, a.length - 1);
    }

    /**
     * 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.
     * @param length the array range length to sort over
     */
    public static final void sort(long[] a, int[] b, int length) {
        mergesort(a, b, 0, length - 1);
    }

    /**
     * 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.
     * @param begin the start index of the range to sort
     * @param end the end index, exclusive, of the range to sort
     */
    public static final void sort(long[] a, int[] b, int begin, int end) {
        mergesort(a, b, begin, end - 1);
    }

    /**
     * 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 void sort(int[] a, int[] b) {
        mergesort(a, b, 0, a.length - 1);
    }

    /**
     * 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.
     * @param length the array range length to sort over
     */
    public static final void sort(int[] a, int[] b, int length) {
        mergesort(a, b, 0, length - 1);
    }

    /**
     * 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.
     * @param begin the start index of the range to sort
     * @param end the end index, exclusive, of the range to sort
     */
    public static final void sort(int[] a, int[] b, int begin, int end) {
        mergesort(a, b, begin, end - 1);
    }

    /**
     * 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. selectionSort(int[] arr)
  2. sort( final Item[] values, final Item[] auxiliary, final int first, final int last)
  3. sort( float[] array)
  4. sort(byte[] b, int pos)
  5. sort(double s[], int idx[])
  6. sort(double[] coords1, int length1, double[] coords2, int length2, int[] array)
  7. sort(double[] data)
  8. sort(double[] data, boolean desc)
  9. sort(Double[] inArray)