Java Quick Sort quickSort(int[] x)

Here you can find the source of quickSort(int[] x)

Description

quick Sort

License

Open Source License

Declaration

public static int[] quickSort(int[] x) 

Method Source Code

//package com.java2s;
/**/*ww  w.ja  v a  2 s. co  m*/
 * Copyright 2004-2006 DFKI GmbH.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * Permission is hereby granted, free of charge, to use and distribute
 * this software and its documentation without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of this work, and to
 * permit persons to whom this work is furnished to do so, subject to
 * the following conditions:
 * 
 * 1. The code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 * 2. Any modifications must be clearly marked as such.
 * 3. Original authors' names are not deleted.
 * 4. The authors' names are not used to endorse or promote products
 *    derived from this software without specific prior written
 *    permission.
 *
 * DFKI GMBH AND THE CONTRIBUTORS TO THIS WORK DISCLAIM ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DFKI GMBH NOR THE
 * CONTRIBUTORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
 * THIS SOFTWARE.
 */

public class Main {
    public static int[] quickSort(int[] x) {
        double[] x2 = new double[x.length];
        int i;
        for (i = 0; i < x.length; i++)
            x2[i] = x[i];

        int[] inds = quickSort(x2);

        for (i = 0; i < x.length; i++)
            x[i] = (int) x2[i];

        return inds;
    }

    public static int[] quickSort(double[] x) {
        int[] indices = new int[x.length];
        for (int i = 0; i < x.length; i++)
            indices[i] = i;

        quickSort(x, indices);

        return indices;
    }

    public static int[] quickSort(double[] x, int startIndex, int endIndex) {
        if (startIndex < 0)
            startIndex = 0;
        if (startIndex > x.length - 1)
            startIndex = x.length - 1;
        if (endIndex < startIndex)
            endIndex = startIndex;
        if (endIndex > x.length - 1)
            endIndex = x.length - 1;

        int[] indices = new int[endIndex - startIndex + 1];
        double[] x2 = new double[endIndex - startIndex + 1];
        int i;

        for (i = startIndex; i <= endIndex; i++) {
            indices[i - startIndex] = i;
            x2[i - startIndex] = x[i];
        }

        quickSort(x2, indices);

        for (i = startIndex; i <= endIndex; i++)
            x[i] = x2[i - startIndex];

        return indices;
    }

    public static void quickSort(double[] x, int[] y) {
        assert x.length == y.length;

        quickSort(x, y, 0, x.length - 1);
    }

    public static void quickSort(double[] x, int[] y, int startIndex, int endIndex) {
        if (startIndex < endIndex) {
            int j = partition(x, y, startIndex, endIndex);
            quickSort(x, y, startIndex, j - 1);
            quickSort(x, y, j + 1, endIndex);
        }
    }

    private static int partition(double[] x, int[] y, int startIndex, int endIndex) {
        int i = startIndex;
        int j = endIndex + 1;
        double t;
        int ty;
        double pivot = x[startIndex];

        while (true) {
            do {
                ++i;
            } while (i <= endIndex && x[i] <= pivot);

            do {
                --j;
            } while (x[j] > pivot);

            if (i >= j)
                break;

            t = x[i];
            ty = y[i];

            x[i] = x[j];
            y[i] = y[j];

            x[j] = t;
            y[j] = ty;
        }

        t = x[startIndex];
        ty = y[startIndex];

        x[startIndex] = x[j];
        y[startIndex] = y[j];

        x[j] = t;
        y[j] = ty;

        return j;
    }
}

Related

  1. quickSort(int[] array, int[] index, int lo0, int hi0)
  2. quicksort(int[] source)
  3. quickSort(int[] source)
  4. quickSort(int[] target, int[] coSort)
  5. quickSort(int[] x)
  6. quickSort(short[] fireZoneInfo, int left, int right)
  7. quicksort(String a[], int lo0, int hi0)
  8. quickSort(String a[], int lo0, int hi0)
  9. quickSort(String[] str, int low, int high)