Here you can find the source of randowPartition(int[] array, int q, int p)
public static int randowPartition(int[] array, int q, int p)
//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; } }