Here you can find the source of randomQuickSort(int[] array)
Parameter | Description |
---|---|
array | a parameter |
public static void randomQuickSort(int[] array)
//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; } }