Java Random Int randomQuickSort(int[] array)

Here you can find the source of randomQuickSort(int[] array)

Description

random quick sort

License

Apache License

Parameter

Parameter Description
array a parameter

Declaration

public static void randomQuickSort(int[] array) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from ww w  . j av  a2  s  .  co m*/
     * random quick sort
     * 
     * @param array
     */
    public static void randomQuickSort(int[] array) {
        randomQuickSort(array, 0, array.length);
    }

    private static void randomQuickSort(int[] array, int start, int end) {
        int r = 0;
        if (start < end) {
            r = randowPartition(array, start, end);
            randomQuickSort(array, start, r);

            randomQuickSort(array, r + 1, end);

        }
    }

    public static int randowPartition(int[] array, int q, int p) {
        int pivotIndex = q + (int) Math.round((p - q - 1) * Math.random());
        // int pivotIndex = q; when pivotIndex == q, this become the normal
        // partition.
        int pivot = array[pivotIndex];

        int[] leftArray = new int[p - q + 1];
        int[] rightArray = new int[p - q + 1];

        // classify
        int ll = 0, rl = 0;
        for (int i = q; i < p; i++) {
            if (array[i] <= pivot && i != pivotIndex) {
                leftArray[ll] = array[i];
                ll++;
            } else if (array[i] > pivot && i != pivotIndex) {
                rightArray[rl] = array[i];
                rl++;
            }
        }

        // combine
        int i = q;
        for (i = q; i < q + ll; i++) {
            array[i] = leftArray[i - q];
        }

        array[i] = pivot;

        i++;

        int returnValue = i - 1;

        for (; i < p; i++) {
            array[i] = rightArray[i - ll - 1 - q];
        }

        return returnValue;

    }
}

Related

  1. randomPass(int n)
  2. randomPointInConvexPoly(float[] pts, int npts, float[] areas, float s, float t)
  3. randomPointsFromFigureEight(int n)
  4. randomPointsFromUnitSphere(int n)
  5. randomPointsFromUnitSquare(int n)
  6. randomRange(int min, int max)
  7. randomSize(int maxValue)
  8. randomSMSCode(int length)
  9. randomStr(int length)