Java Array Sort sort3(String[] arr, float[] brr)

Here you can find the source of sort3(String[] arr, float[] 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 sort3(String[] arr, float[] brr) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   ww w.  ja va2s .co  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 sort3(String[] arr, float[] brr) {
        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 (;;) {
            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].compareTo(a) <= 0)
                            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;
                dummyString = arr[k];
                arr[k] = arr[l + 1];
                arr[l + 1] = dummyString;

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

                if (arr[l].compareTo(arr[ir]) > 0) {
                    dummyString = arr[l];
                    arr[l] = arr[ir];
                    arr[ir] = dummyString;

                    dummyFloat = brr[l];
                    brr[l] = brr[ir];
                    brr[ir] = dummyFloat;

                }
                if (arr[l + 1].compareTo(arr[ir]) > 0) {
                    dummyString = arr[l + 1];
                    arr[l + 1] = arr[ir];
                    arr[ir] = dummyString;

                    dummyFloat = brr[l + 1];
                    brr[l + 1] = brr[ir];
                    brr[ir] = dummyFloat;
                }
                if (arr[l].compareTo(arr[l + 1]) > 0) {
                    dummyString = arr[l];
                    arr[l] = arr[l + 1];
                    arr[l + 1] = dummyString;

                    dummyFloat = brr[l];
                    brr[l] = brr[l + 1];
                    brr[l + 1] = dummyFloat;
                }
                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].compareTo(a) < 0);
                    do
                        j--;
                    while (arr[j].compareTo(a) > 0);
                    if (j < i)
                        break; // Pointers crossed. Partitioning complete.
                    dummyString = arr[i];
                    arr[i] = arr[j];
                    arr[j] = dummyString;
                    dummyFloat = brr[i];
                    brr[i] = brr[j];
                    brr[j] = dummyFloat;
                } // 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. sort1(int x[], int off, int len, int y[])
  2. sort1(long x[], int off, int len)
  3. sort2(double v[], int v2[], int left, int right)
  4. sort2(double[] arr, double[] brr)
  5. sort3(String[] a, int x, int y, int z)
  6. sort5(Long[] data)
  7. sortableBytesToInt(byte[] encoded, int offset)
  8. sortAccording(float[] ary, Integer[] sortOrder)
  9. sortAccording2(int[] ary, Integer[] sortOrder)