Java Quick Sort quicksort(double[] main, int[] index)

Here you can find the source of quicksort(double[] main, int[] index)

Description

quicksort

License

Open Source License

Declaration

public static void quicksort(double[] main, int[] index) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static void quicksort(double[] main, int[] index) {
        quicksort(main, index, 0, index.length - 1);
    }//from   w  ww .j av  a2s.c  o  m

    public static void quicksort(double[] a, int[] index, int left, int right) {
        if (right <= left)
            return;
        int i = partition(a, index, left, right);
        quicksort(a, index, left, i - 1);
        quicksort(a, index, i + 1, right);
    }

    private static int partition(double[] a, int[] index, int left, int right) {
        int i = left - 1;
        int j = right;
        while (true) {
            while (less(a[++i], a[right])) // find item on left to swap
                ; // a[right] acts as sentinel
            while (less(a[right], a[--j])) // find item on right to swap
                if (j == left)
                    break; // don't go out-of-bounds
            if (i >= j)
                break; // check if pointers cross
            exch(a, index, i, j); // swap two elements into place
        }
        exch(a, index, i, right); // swap with partition element
        return i;
    }

    private static boolean less(double x, double y) {
        double first = Double.isNaN(x) ? Double.NEGATIVE_INFINITY : x;
        double second = Double.isNaN(y) ? Double.NEGATIVE_INFINITY : y;
        return (first > second);
    }

    private static void exch(double[] a, int[] index, int i, int j) {
        double swap = a[i];
        a[i] = a[j];
        a[j] = swap;
        int b = index[i];
        index[i] = index[j];
        index[j] = b;
    }
}

Related

  1. quickSort( float[] array, int[] index, int left, int right)
  2. quicksort(Comparable[] a)
  3. quicksort(double lista1[], int lista2[], int izq, int der)
  4. quickSort(double[] array, int low, int high)
  5. quickSort(double[] array, int[] idx, int from, int to)
  6. quicksort(final double[] array, final int[] index)
  7. quicksort(final long[] data, final int left, final int right)
  8. quicksort(float[] a, int[] index, int left, int right)
  9. quickSort(int[] a)