Java Array Sort sort2(double[] arr, double[] brr)

Here you can find the source of sort2(double[] arr, double[] brr)

Description

Sorts an array arr[1..n] into ascending order using Quicksort, while making the corresponding rearrangement of the array brr[1..n].

License

Open Source License

Declaration

public static void sort2(double[] arr, double[] brr) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from www  .  j a  v  a2s  .  c  o  m
     * Sorts an array arr[1..n] into ascending order using Quicksort,
     * while making the corresponding rearrangement of the array brr[1..n].
     */
    public static void sort2(double[] arr, double[] brr) {
        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 (;;) {
            if (ir - l < 7) {
                for (j = l + 1; j <= ir; j++) {
                    a = arr[j];
                    b = brr[j];
                    for (i = j - 1; i >= l; i--) {
                        if (arr[i] <= a)
                            break;
                        arr[i + 1] = arr[i];
                        brr[i + 1] = brr[i];
                    }
                    arr[i + 1] = a;
                    brr[i + 1] = b;
                }
                if (jstack == 0) {
                    istack = null;
                    return;
                }
                ir = istack[jstack];
                l = istack[jstack - 1];
                jstack -= 2;
            } else {
                k = (l + ir) >> 1;
                dummy = arr[k];
                arr[k] = arr[l + 1];
                arr[l + 1] = dummy;

                dummy = brr[k];
                brr[k] = brr[l + 1];
                brr[l + 1] = dummy;

                if (arr[l] > arr[ir]) {
                    dummy = arr[l];
                    arr[l] = arr[ir];
                    arr[ir] = dummy;

                    dummy = brr[l];
                    brr[l] = brr[ir];
                    brr[ir] = dummy;

                }
                if (arr[l + 1] > arr[ir]) {
                    dummy = arr[l + 1];
                    arr[l + 1] = arr[ir];
                    arr[ir] = dummy;

                    dummy = brr[l + 1];
                    brr[l + 1] = brr[ir];
                    brr[ir] = dummy;
                }
                if (arr[l] > arr[l + 1]) {
                    dummy = arr[l];
                    arr[l] = arr[l + 1];
                    arr[l + 1] = dummy;

                    dummy = brr[l];
                    brr[l] = brr[l + 1];
                    brr[l + 1] = dummy;
                }
                i = l + 1; // Initialize pointers for partitioning.
                j = ir;
                a = arr[l + 1]; //Partitioning element.
                b = brr[l + 1];
                for (;;) { // Beginning of innermost loop.
                    do
                        i++;
                    while (arr[i] < a);
                    do
                        j--;
                    while (arr[j] > a);
                    if (j < i)
                        break; // Pointers crossed. Partitioning complete.
                    dummy = arr[i];
                    arr[i] = arr[j];
                    arr[j] = dummy;
                    dummy = brr[i];
                    brr[i] = brr[j];
                    brr[j] = dummy;
                } // End of innermost loop.
                arr[l + 1] = arr[j]; // Insert partitioning element in both arrays.
                arr[j] = a;
                brr[l + 1] = brr[j];
                brr[j] = b;
                jstack += 2;
                if (jstack > 50)
                    System.out.println("NSTACK too small in sort2.");
                if (ir - i + 1 >= j - l) {
                    istack[jstack] = ir;
                    istack[jstack - 1] = i;
                    ir = j - 1;
                } else {
                    istack[jstack] = j - 1;
                    istack[jstack - 1] = l;
                    l = i;
                }
            }
        }
    }

    /**
     * Sorts an array arr[1..n] into ascending order using Quicksort,
     * while making the corresponding rearrangement of the array brr[1..n].
     */
    public static void sort2(float[] arr, float[] brr) {
        int n = arr.length;
        int i, ir = n - 1, j, k, l = 0;
        int[] istack;
        int jstack = 0;
        float a, b, temp;
        float dummy;
        istack = new int[50];
        for (;;) {
            if (ir - l < 7) {
                for (j = l + 1; j <= ir; j++) {
                    a = arr[j];
                    b = brr[j];
                    for (i = j - 1; i >= l; i--) {
                        if (arr[i] <= a)
                            break;
                        arr[i + 1] = arr[i];
                        brr[i + 1] = brr[i];
                    }
                    arr[i + 1] = a;
                    brr[i + 1] = b;
                }
                if (jstack == 0) {
                    istack = null;
                    return;
                }
                ir = istack[jstack];
                l = istack[jstack - 1];
                jstack -= 2;
            } else {
                k = (l + ir) >> 1;
                dummy = arr[k];
                arr[k] = arr[l + 1];
                arr[l + 1] = dummy;

                dummy = brr[k];
                brr[k] = brr[l + 1];
                brr[l + 1] = dummy;

                if (arr[l] > arr[ir]) {
                    dummy = arr[l];
                    arr[l] = arr[ir];
                    arr[ir] = dummy;

                    dummy = brr[l];
                    brr[l] = brr[ir];
                    brr[ir] = dummy;

                }
                if (arr[l + 1] > arr[ir]) {
                    dummy = arr[l + 1];
                    arr[l + 1] = arr[ir];
                    arr[ir] = dummy;

                    dummy = brr[l + 1];
                    brr[l + 1] = brr[ir];
                    brr[ir] = dummy;
                }
                if (arr[l] > arr[l + 1]) {
                    dummy = arr[l];
                    arr[l] = arr[l + 1];
                    arr[l + 1] = dummy;

                    dummy = brr[l];
                    brr[l] = brr[l + 1];
                    brr[l + 1] = dummy;
                }
                i = l + 1; // Initialize pointers for partitioning.
                j = ir;
                a = arr[l + 1]; //Partitioning element.
                b = brr[l + 1];
                for (;;) { // Beginning of innermost loop.
                    do
                        i++;
                    while (arr[i] < a);
                    do
                        j--;
                    while (arr[j] > a);
                    if (j < i)
                        break; // Pointers crossed. Partitioning complete.
                    dummy = arr[i];
                    arr[i] = arr[j];
                    arr[j] = dummy;
                    dummy = brr[i];
                    brr[i] = brr[j];
                    brr[j] = dummy;
                } // End of innermost loop.
                arr[l + 1] = arr[j]; // Insert partitioning element in both arrays.
                arr[j] = a;
                brr[l + 1] = brr[j];
                brr[j] = b;
                jstack += 2;
                if (jstack > 50)
                    System.out.println("NSTACK too small in sort2.");
                if (ir - i + 1 >= j - l) {
                    istack[jstack] = ir;
                    istack[jstack - 1] = i;
                    ir = j - 1;
                } else {
                    istack[jstack] = j - 1;
                    istack[jstack - 1] = l;
                    l = i;
                }
            }
        }
    }
}

Related

  1. sort(T[] array, boolean nullsFirst)
  2. sort(T[] src)
  3. sort1(int x[], int off, int len, int y[])
  4. sort1(long x[], int off, int len)
  5. sort2(double v[], int v2[], int left, int right)
  6. sort3(String[] a, int x, int y, int z)
  7. sort3(String[] arr, float[] brr)
  8. sort5(Long[] data)
  9. sortableBytesToInt(byte[] encoded, int offset)