Java Merge Sort mergesort(double[] a, int[] b, int p, int r)

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

Description

Arrays with lengths beneath this value will be insertion sorted.

License

Open Source License

Declaration

protected static final void mergesort(double[] a, int[] b, int p, int r) 

Method Source Code

//package com.java2s;

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

    /**
     * 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. mergeSort(int[] a)
  2. mergeSort(int[] array)
  3. mergeSort(long[] theArray, int nElems)
  4. mergeSort(Object[] src, Object[] dest, int low, int high, int off)