Java Random Int randowPartition(int[] array, int q, int p)

Here you can find the source of randowPartition(int[] array, int q, int p)

Description

randow Partition

License

Apache License

Declaration

public static int randowPartition(int[] array, int q, int p) 

Method Source Code

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

public class Main {
    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++;//from  w w w  .j  a v  a  2 s  .c o m
            } 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. randomText(int length)
  2. randomWithinRange(int min, int max)
  3. randomWithRange(int min, int max)
  4. randomWord(int wordLength)
  5. randomZeroString(int length)
  6. randProductNumerid(final int c, final int count)
  7. RandSelect(int iTotal)