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;
/**// w w  w. j a v  a 2  s  . c  o m
 * Copyright 2004-2006 DFKI GmbH.
 * All Rights Reserved.  Use is subject to license terms.
 *
 * This file is part of MARY TTS.
 *
 * MARY TTS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

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(float[] 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 int[] quickSort(float[] 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];
        float[] x2 = new float[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(float[] 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);
        }
    }

    public static void quickSort(float[] 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;
    }

    private static int partition(float[] x, int[] y, int startIndex, int endIndex) {
        int i = startIndex;
        int j = endIndex + 1;
        float t;
        int ty;
        float 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 lo0, int hi0)
  2. quickSort(int[] array, int[] index, int lo0, int hi0)
  3. quicksort(int[] source)
  4. quickSort(int[] source)
  5. quickSort(int[] target, int[] coSort)
  6. quickSort(int[] x)
  7. quickSort(short[] fireZoneInfo, int left, int right)
  8. quicksort(String a[], int lo0, int hi0)
  9. quickSort(String a[], int lo0, int hi0)